Selenium Webdriver in Python - files download directory change in Chrome preferences
I'm using Selenium Webdriver (in Python) to automate the downloading of thousands of files. I want to set Chrome's download folder programmatically. After reading this, I tried this:
chromepath = '/Users/thiagomarzagao/Desktop/searchcode/chromedriver'
desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/'}}}
driver = webdriver.Chrome(executable_path = chromepath, desired_capabilities = desired_caps)
No good. Downloads still go to the default download folder ("/Users/thiagomarzagao/Downloads").
Any thoughts?
(Python 2.7.5, Selenium 2.2.0, Chromedriver 2.1.210398, Mac OS X 10.6.8)
Solution 1:
The following worked for me:
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "/some/path"}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = "path/to/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver, options=chromeOptions)
Source: https://sites.google.com/a/chromium.org/chromedriver/capabilities
Solution 2:
If anyone is still having trouble and the above solutions didn't work, I found adding a following slash ('\') to my download path.
Mine looked like this:
if browser == 'chrome':
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
prefs = {"profile.default_content_settings.popups": 0,
"download.default_directory": r"C:\Users\user_dir\Desktop\\", # IMPORTANT - ENDING SLASH V IMPORTANT
"directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
return webdriver.Chrome(executable_path=Base.chromedriver_dir, chrome_options=options)
Solution 3:
I think you also need
"directory_upgrade": true
Using the dictionary directly in a Chrome 'Prefrences' file, on a local windows install of chrome Version 28.0.1500.95 m, with the following download options:
"download": {
"default_directory": "C:\\Users\\rdub\\Desktop",
"extensions_to_open": ""
},
I get the default location, versus the desktop. When I change it to this:
"download": {
"default_directory": "C:\\Users\\rdub\\Desktop",
"directory_upgrade": true,
"extensions_to_open": ""
},
I get the desktop location.
Try the following:
desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/', "directory_upgrade": true, "extensions_to_open": ""}}}
Solution 4:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
temp_directory = ""
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--verbose')
chrome_options.add_experimental_option("prefs", {
"download.default_directory": "<path to the folder of download>",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False
})
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-software-rasterizer')
url = "https://www.thinkbroadband.com/download"
driver = webdriver.Chrome(executable_path = './chromedriver' ,chrome_options = chrome_options)
driver.get(url)
time.sleep(5)
driver.find_element_by_css_selector("div.module:nth-child(8) > p:nth-child(1) > a:nth-child(1) > img:nth-child(1)").click()
Solution 5:
I try all the anwsers in this question, but it doesn't work for my in Ubuntu 16.10. So I add the change with os.environ for the variable XDG_DOWNLOAD_DIR. Which doesn't work, but I think that it helps.
That is:
os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory
The really change that works perfectly for me is setup the download folder via the command xdg-user-dirs-update through a system call in execution time:
os.system("xdg-user-dirs-update --set DOWNLOAD " + default_download_directory)
So, all my code related to setup the download dir is the following:
import os
from selenium import webdriver
default_download_directory = str(os.path.dirname(os.path.abspath(__file__))) + "/download"
os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory
os.system("xdg-user-dirs-update --set DOWNLOAD " + default_download_directory)
desired_caps = {
'prefs': {
'download': {
'default_directory': str(os.path.dirname(os.path.abspath(__file__))) + "/download",
"directory_upgrade": "true",
"extensions_to_open": ""
}
}
}
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=" + str(os.path.dirname(os.path.abspath(__file__))) + "/download")
browser = webdriver.Chrome(chrome_options=options, desired_capabilities=desired_caps)