Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium Webdriver - Dynamically change download directory

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?

like image 285
user3294195 Avatar asked Mar 29 '14 17:03

user3294195


People also ask

How to download files to a specified location with selenium Python?

We can download files to a specified location with Selenium in Python. This is done by the help of the ChromeOptions class. We shall set the preferences of the browser and pass the download.default_directory parameter.

What is Selenium Webdriver in Python?

Selenium WebDriver is one of the most popular tools for Web UI Automation. Selenium is an open source tool, and its library is available in different programming languages to perform the Web UI Automation testing, and Python is one of them. How much Python knowledge is required for Selenium?

Why can't I set the download directory via WebDriver?

Solution Re: Set download directory via WebDriver @Marcel_Holle thanks for the code, that helps so much. The issue is because you are using ChromeDriver() which is not supported on Edge 81 and newer.

What is add_experimental_option in selenium?

options: Helps set the preferences to Chrome browser. download.default_directory : Used for changing the default download directory. Example: The code specifies C:\Tutorial\down, which means that the file will be downloaded to that location. add_experimental_option: Allows users to add these preferences to their Selenium webdriver object.


2 Answers

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
like image 172
Mark Beebe Avatar answered Sep 26 '22 02:09

Mark Beebe


I just tried this way to change download folder during webdriver running:

driver.command_executor._commands['send_command'] = (
    'POST', '/session/$sessionId/chromium/send_command')
download_path = 'PATH/TO/MY/CURRENT/DESIRED'
params = {
    'cmd': 'Page.setDownloadBehavior',
    'params': { 'behavior': 'allow', 'downloadPath': download_path }
}
driver.execute("send_command", params)

or:

download_path = 'PATH/TO/MY/CURRENT/DESIRED'
params = { 'behavior': 'allow', 'downloadPath': download_path }
driver.execute_cdp_cmd('Page.setDownloadBehavior', params['params'])

It won't change Chrome's default download location setting, but will save file to the new given folder (will create if not exists) in subsequent downloads.

like image 29
calfzhou Avatar answered Sep 25 '22 02:09

calfzhou