Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default download folder while webdriver is running?

I am downloading several different data sets and would like each file (or set) to download to a specific folder. I have learned how to change the download directories at these page:

setting Chrome preferences w/ Selenium Webdriver in Python

Change the default chrome download folder webdriver C#

The problem is these methods only allow me to change the download directory when I open the webdriver. It takes a while to get to the download page so doing this is an ineffective solution. I've tried set preferences but I'm working with selenium webdriver and chrome in python and I have not been able to find anything on SO or in the python help. Even switching the window handle on a new driver won't work because it cannot grab another driver's already open window.

The link for the download site is customized so can't copy and paste into a new driver either. So far I've been using the os. module to get the name of each new file coming in but even this is unreliable because of varying download times.

If anyone has any idea on how to change the default settings to a webdriver while the webdriver is running that would be great. Thanks!

like image 459
seeiespi Avatar asked May 27 '14 18:05

seeiespi


1 Answers

In the past, I have solved this by downloading to a temp folder and then renaming the file to the appropriate folder with something along the line of this:

def move_to_download_folder(downloadPath, newFileName, fileExtension):
    got_file = False   
    ## Grab current file name.
    while got_file = False:
        try: 
            currentFile = glob.glob(DOWNLOAD_PATH+"*"+fileExtension)
            got_file = True

        except:
            print "File has not finished downloading"
            time.sleep(20)

    ## Create new file name
    fileDestination = downloadPath+newFileName+fileExtension

    os.rename(currentFile, fileDestination)

    return

## Click element to download file
inputElement=driver.find_element_by_xpath("{xpath here}").click()

move_to_download_folder(downloadPath, newFileName, fileExtension)
like image 58
ExperimentsWithCode Avatar answered Nov 11 '22 11:11

ExperimentsWithCode