Selenium Webdriver in Python - Files Download Directory Change in Chrome Preferences

Downloading a file at a specified location through python and selenium using Chrome driver

Update 2018:

Its not valid Chrome command line switch, see the source code use hoju answer below to set the Preferences.

Original:

You can create a profile for chrome and define the download location for the tests. Here is an example:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=C:/Downloads")

driver = webdriver.Chrome(chrome_options=options)

How to download files in customized location using Selenium ChromeDriver and Chrome

To download the required file within Automation Demo Site to a specific folder using Selenium, ChromeDriver and google-chrome you need to pass the preference "download.default_directory" along with the value (location of the directory) through add_experimental_option() and you can use the following solution:

Code Block:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Data_Files\output_files"
})
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("http://demo.automationtesting.in/FileDownload.html")
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Download"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "textbox"))).send_keys("testing")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "createTxt"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "link-to-download"))).click()

Snapshot:

chrome_download

Selenium Chrome: Load profile and change download folder - Python

I believe there isn't a way to both load an existing profile and also to change the default_directory option.

So instead, I used json.loads and json.dump to modify the 'Preference' file before loading the profile.


import json
from selenium import webdriver

# helper to edit 'Preferences' file inside Chrome profile directory.
def set_download_directory(profile_path, profile_name, download_path):
prefs_path = os.path.join(profile_path, profile_name, 'Preferences')
with open(prefs_path, 'r') as f:
prefs_dict = json.loads(f.read())
prefs_dict['download']['default_directory'] = download_path
prefs_dict['savefile']['directory_upgrade'] = True
prefs_dict['download']['directory_upgrade'] = True
with open(prefs_path, 'w') as f:
json.dump(prefs_dict, f)

options = webdriver.ChromeOptions()
set_download_directory(profile_path, profile_name, download_path) # Edit the Preferences first before loading the profile.
options.add_argument(f'user-data-dir={profile_path}')
options.add_argument(f'--profile-directory={profile_name}')
driver = webdriver.Chrome(chrome_options=options)

How to use Selenium via Python on Chrome to change multiple downloaded file directories without having to launch the web driver & link more than once?

You can use the driver.command_executor method to achieve this. It allows you to interact with the current browser session.
You can use this method to change the download path, without relaunching the web driver.

The code snippet is given below-

You can change the 'downloadPath' parameter as per your requirement.

#initially setting the download path to current directory
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow','downloadPath':os.getcwd()}}
command_result = driver.execute("send_command", params)

#your code to download the file

#followed by changing the download directory
#for example here I'm changing it to data folder inside the current working directory

driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow','downloadPath':os.getcwd()+'\data'}}
command_result = driver.execute("send_command", params)


Related Topics



Leave a reply



Submit