【问题标题】:Python inner and outer for loopPython内部和外部for循环
【发布时间】: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


【解决方案1】:

您可以将整个代码包装成这样的函数:

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

 

【讨论】:

  • 以及您想再次运行的任何代码部分:将其包装到一个函数中,然后在任何您想调用的地方简单地调用它
  • 当然,在内部循环中,您可以根据需要使用自己的逻辑:例如if - else 条件等。
  • 嗨@maxt026,感谢您的回复!你的代码对我有用!然而,我遇到了另一个问题:“followLoop”不再增加 XPath。你碰巧知道如何解决这个问题吗?我更新了帖子中的代码。
  • 很高兴知道我提供了帮助 :) 你能支持我的回答吗?我想在 StackOF 上提高一点我的声誉
  • 在调用后你想重新定义一个变量:你可以使函数参数化
猜你喜欢
  • 1970-01-01
  • 2020-07-09
  • 2012-05-11
  • 2013-06-07
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2015-03-21
  • 2016-08-31
相关资源
最近更新 更多