Python Selenium Webdriver - Dynamically change download directory

python, selenium

Solution

I have been unable to figure out how to do this and have used a work around. Instead of changing the webDriver download directory on the fly, the below solution just moves the file that you download.

ExperimentsWithCode Gave the Answer Here. Below is part of his solution

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

Problem

To explicitly define the download directory prior to defining the selenium webdriver we use the following code: ``` chromeOptions = webdriver.ChromeOptions() prefs = {"download.default_directory" : "C:/data/cline"} chromeOptions.add_experimental_option("prefs",prefs) chromePath = "path to chromedriver" driver = selenium.webdriver.chrome.webdriver.WebDriver(executable_path=chromePath, port=0, chrome_options=chromeOptions, service_args=None, desired_capabilities=None, service_log_path=None) ``` I want to download a number of files, each to a different (newly created) directory. Is it possible to change the download directory after defining driver?

Original source

Related problems