【问题标题】:Save downloaded files in folder (Python)将下载的文件保存在文件夹中(Python)
【发布时间】:2021-06-20 16:54:09
【问题描述】:

我有以下代码。当内部循环完成(中断)时,我希望将下载的文件放在特定文件夹中。我希望每次执行循环并遇到异常时都执行此操作。有谁知道如何做到这一点?

任何帮助将不胜感激!提前致谢!

#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(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\\'
                
                testLoop = range(1, 10)
                for i in testLoop:
                    dst = dst + str(i)

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

loop_function()

【问题讨论】:

  • 那里没有下载文件...
  • #Click print printButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_btnPrintBehandelverloop"]') printButton.click()此代码下载一个pdf文件。
  • OK,它将文件下载到默认/预定义目录。现在您想将该文件复制到其他位置吗?您的项目中定义的下载文件路径是什么?您希望将这些文件移到哪里?
  • 我希望每次都将文件移动到不同的文件夹。例如:如果循环第一次执行,我希望将“下载”中的文件移动到文件夹“1”。如果第二次执行循环,“下载”文件夹应该是空的。然后我希望将下载中的所有文件移动到文件夹“2”。我希望你明白我的意思。感谢您与我一起思考!

标签: python selenium loops automation shutil


【解决方案1】:

当您通过代码下载文件时

printButton = driver.find_element_by_xpath('//*[@id="ctl00_CPH_Main_ctl00_Verslagen_btnPrintBehandelverloop"]') 
printButton.click()`

它将文件下载到您在驱动程序选项中定义的下载文件夹。
假设默认文件夹是C:/Downloads,您想将文件移动到文件夹C:/Folder1
假设您下载的每个文件都以“my_file”开头

你要做的是:

import os
from os import path
import shutil

src = `C:/Downloads`
dst = `C:/Folder1`

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

【讨论】:

  • 非常感谢!我希望在复制后删除所有文件,因此对于第二个循环,第一个循环中的文件也不会再次复制。我怎么能这样做?文件也可以剪切和粘贴吗?
  • 抱歉,您应该使用move 而不是copy 从原始文件夹中删除文件。更新了答案
  • 谢谢!我现在有这个代码(请参阅更新的代码)。但是现在下载不会出现在“下载”中,但也不会出现在文件夹中。你知道我做错了吗?
  • 嗯...我认为您应该调试它以查看文件现在要去哪里。我在我这里看不到这个..
  • 使用当前代码,所有文件都粘贴到文件夹“1”中,我认为似乎不会增加路径?但是如何解决这个问题?
猜你喜欢
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多