首页 > 解决方案 > 如何将下载放在正确的文件夹中?

问题描述

现在,我有底层代码。简而言之:它下载文件并将所述文件放入文件夹中。我遇到的问题如下:目前,下载被放入文件夹 x+1 (这是客户端 ID)。x+1 值并不总是正确的值(客户端 ID),因为一些客户端已被删除。该网站确实显示了这样的客户端 ID 文本: <span id="ctl00_CPH_Main_ctl00_lblID" class="invoer_label">2</span><input name="ctl00$CPH$Main$ctl00$TxtPatientNr" type="text" value="2" readonly="readonly" id="TxtPatientNr" class="textbox_algemeen_klein textbox_phone_full_width textbox_tablet_50procent">,其中“2”是实际的客户端 ID。我可以使用此值将下载的文件放入正确的文件夹吗?或者您知道如何确保下载文件放在正确的文件夹中吗?

#Function to export data
def loop_function():

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

    #Click on show filter
    showCustomerFilter = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00_ctl03_ctl01_PageSizeComboBox_Arrow"]')
    showCustomerFilter.click()

    time.sleep(5)

    #Click on show 1000
    selectCustomerFilter = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00_ctl03_ctl01_PageSizeComboBox_DropDown"]/div/ul/li[6]')
    selectCustomerFilter.click()    

    #Loop client ID's
    followLoop = range(985, 1015)
    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 i in followLoop2:
            try:
                xpath2 = '//*[@id="ctl00_CPH_Main_ctl00_RadGrid1_ctl00__'
                xpath2 += str(i)
                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(1)

                #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()

                #Load files to be downloaded
                time.sleep(1)

                #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:
                src = 'C:\\Users\\sohan\\Downloads'
                dst = 'C:\\Users\\sohan\\Documents\\Werk\\BIA Solutions\\Huid & Laserkliniek Delft\\Data\\' + str(x+1)

                files = [i for i in os.listdir(src) if i.startswith("behandel") and path.isfile(path.join(src, i))]
                for f in files:
                    shutil.move(path.join(src, f), dst)

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

                #Click on show filter
                showCustomerFilter = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00_ctl03_ctl01_PageSizeComboBox_Arrow"]')
                showCustomerFilter.click()

                time.sleep(5)

                #Click on show 1000
                selectCustomerFilter = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_RadGrid_Patienten_ctl00_ctl03_ctl01_PageSizeComboBox_DropDown"]/div/ul/li[6]')
                selectCustomerFilter.click()     
                break

loop_function()

标签: pythonselenium

解决方案


推荐阅读