【发布时间】:2021-06-16 14:25:50
【问题描述】:
我首先希望代码执行第一个循环。完成后,我希望代码执行内部循环,它确实如此。但是当没有选项运行内循环时,我希望代码回到外循环,但它不会这样做。
有谁知道我如何做到这一点?
def loop_function():
#Search client
searchCustomerButton = driver.find_element_by_xpath('//*[@id="ibSearchPatient"]')
searchCustomerButton.click()
followLoop = range(0, 10)
for x in followLoop:
xpath = '//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00__'
xpath += str(x)
xpath += '"]/td[3]'
#Click on cliënt ID
driver.find_element_by_xpath(xpath).click()
#Click on Zorgtraject
zorgtrajectButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[3]/a/span/span/span')
zorgtrajectButton.click()
followLoop2 = range(0,10)
for x in followLoop2:
# begin the inner loop with:
try:
# inner loop code here
xpath2 = '//*[@id="ctl00_CPH_Main_ctl00_RadGrid1_ctl00__'
xpath2 += str(x)
xpath2 += '"]/td[2]'
#Click on Zorgtraject ID
driver.find_element_by_xpath(xpath2).click()
#Dossier button
dossierButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[5]/a/span/span/span')
dossierButton.click()
#Dropdown select
dropdownSelector = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl03_ctl01_PageSizeComboBox_Arrow"]')
dropdownSelector.click()
#Prevent not interactable error
time.sleep(2)
#Select 50
selectDropdown = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl03_ctl01_PageSizeComboBox_DropDown"]/div/ul/li[4]')
selectDropdown.click()
#Check all documents
tickDossier = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_radGridBehandelVerloop_ctl00_ctl02_ctl01_headerChkboxPrint"]')
tickDossier.click()
#Click print
printButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_btnPrintBehandelverloop"]')
printButton.click()
#Click on Zorgtraject ID
zorgtrajectButton2 = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Top_ToolBar_MenuCurrentPage"]/div/ul/li[3]/a/span/span/span')
zorgtrajectButton2.click()
except NoSuchElementException: # define here what type of exception is thrown when there is no option to run the inner loop
loop_function() # call the function once again if you want to run the outer loop again
exec(loop_function())
【问题讨论】:
-
您是否发现任何异常?运行时的实际流程是什么?
-
1.它检查是否有客户端。 2.它检查是否有护理轨迹。 3.它点击档案。 4. 导出所有档案。 5.它(应该)检查是否有另一个护理轨迹。如果是这样,它应该执行步骤 4 和 5。 6. 当没有更多的护理轨迹/档案时,它应该返回选择一个新的客户并再次执行这些步骤。
标签: python selenium loops for-loop automation