XPath 递增 Python

XPath increment with Python

我正在尝试增加变量 'xpath',但它不适用于下面的代码。有谁知道我该如何解决这个问题?如果我不使用这个函数,内部循环就不起作用,所以我需要保持那个部分(据我所知)。

如有任何帮助,我们将不胜感激!提前致谢!

#Function to export data
def loop_function():

    #Search client
    searchCustomerButton = driver.find_element_by_xpath('//*[@id="ibSearchPatient"]')
    searchCustomerButton.click()    

    #Loop client ID's
    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()

        #Loop Zorgtraject ID's
        followLoop2 = range(0,10)
        for x in followLoop2:
            try:
                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()

            #If no Zorgtraject ID, start function over
            except NoSuchElementException:
                loop_function()
                
exec(loop_function())

您尝试过使用 f-string 吗?

xpath = f'//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00__{x}"]/td[3]'

但是,如果此模式始终为真,我更愿意使用 starts-with 一次查找所有元素,然后循环。

xpath = '//*[starts-with(@id, "ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00__")]'

我猜你可以完成你想要的循环元素并在 NoSuchElementException:

时中断第二个循环
# Function to export data
def loop_function():
    # Search client
    searchCustomerButton = driver.find_element_by_xpath('//*[@id="ibSearchPatient"]')
    searchCustomerButton.click()

    # Loop client ID's
    followLoop = range(0, 10)
    for x in followLoop:
        xpath = f'//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00__{x}"]/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()

        # Loop Zorgtraject ID's
        followLoop2 = range(0, 10)
        for x in followLoop2:
            try:
                xpath2 = f'//*[@id="ctl00_CPH_Main_ctl00_RadGrid1_ctl00__{x}"]/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()

            # If no Zorgtraject ID, break the inner loop 
            # and continue the outer loop iteration
            except NoSuchElementException:
                break