python - Python内部和外部for循环
问题描述
我首先希望代码执行第一个循环。完成后,我希望代码执行它的内部循环。但是当没有选择运行内循环时,我希望代码回到外循环,但它不会那样做。
有谁知道我怎么能做到这一点?
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())
解决方案
您可以将整个代码包装成这样的函数:
def loop_function():
followLoop = range(0, 10)
for x in followLoop:
# outer loop code here
for x in followLoop2:
# begin the inner loop with:
try:
# inner loop code here
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
推荐阅读
- azure-functions - 使用 MSI 对启用了防火墙的 eventthub 的 Azure 功能消费计划调用
- wso2 - WSO2 Vault-lookup Xpath 表达式返回 Empty/Blank
- google-cloud-platform - 运行 Dataproc 工作流
- r - 如何使用 group_by 和 case_when 在列中估算新值?
- keras - Tenserflow Keras 模型不接受我的 python 生成器输出
- javascript - 将变量值传入和传出内部 iframe
- google-cloud-platform - 使用 terraform 和自定义层创建 CloudSQL 实例
- sql-server - 尝试创建一个带有错误捕获的存储过程,该过程将在执行时打印错误
- excel - 根据匹配条件复制值
- flutter - 如何在 Flutter 中打开特定屏幕