用Python实现XPath增量

2024-09-29 23:22:56 发布

您现在位置: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())

Tags: idbymaindriverfunctionelementfindxpath
1条回答
网友
1楼 · 发布于 2024-09-29 23:22:56

你试过使用f字串吗

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__")]'

我想您可以通过元素循环并在NoTouchElementException时打破第二个循环来完成您想要的:

# 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

相关问题 更多 >

    热门问题