Shin Shan
Shin Shan

Reputation: 17

Save downloaded files in folder (Python)

I have the following code. When the inner loop has finished (breaks), I want the downloaded files to be placed in a specific folder. I want this to be done for every time the loop gets executed and the exception is reached. Does anyone know how to achieve this?

Any help would be appreciated! Thanks in advance!

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

Upvotes: 0

Views: 954

Answers (1)

Prophet
Prophet

Reputation: 33361

When you download the file by your code with

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

it downloads the file to the downloads folder you defined in your driver options.
Let's say the default folder is C:/Downloads and you want to move the files to folder C:/Folder1
Let's say each your downloaded file starts with "my_file"

What you have to do is:

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)

Upvotes: 1

Related Questions