Reputation: 2064
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)
Upvotes: 58
Views: 145342
Reputation: 11
This work in 2024 with windows chromedriver 121.0.6167.85
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from time import sleep
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : r"C:\Users\adm\OneDrive\\"}
chromeOptions.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(options=chromeOptions)
Headless = False
browser.get("url")
sleep (2)
Upvotes: 1
Reputation: 159
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
chrome_options = Options()
# chrome_options.add_argument('--headless') # Add any additional options if needed
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument('--disable-dev-shm-usage')
#
chrome_options.add_experimental_option('prefs', {
'download.default_directory': 'C:\\Users\\61478\\PycharmProjects\\WebScraper\\',
#'download.default_directory': r'C:\Users\61478\PycharmProjects\WebScraper\\',
'directory_upgrade': True,
'download.directory_upgrade': True,
'safebrowsing.enabled': True
})
# Pass the combined options to the Chrome WebDriver
driver = webdriver.Chrome(chrome_options)
driver.maximize_window()
driver.get('https://fastest.fish/test-files')
time.sleep(1)
# Wait for the download link to be clickable
download_link = driver.find_element(By.XPATH, "(//*[@class=\"table\"]/tbody/tr/td/a)[1]")
download_link.click()
# Let the download complete
time.sleep(10) # Adjust this wait time as needed
# Close the browser
driver.quit()
#
# Both path worked for me
# Spend some time tweaking this, finally find it.
#
# Chrome browser version: 120.0.6099.71
# Selenium Version: 4.15.2
Upvotes: 0
Reputation: 3
It worked for me using the following code:
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
prefs = {'profile.default_content_settings.popups': 0,
'download.default_directory' : r'C:\Users\user\folder\\',
'directory_upgrade': True}
options.add_experimental_option('prefs', prefs)
chromedriver = r"C:\Users\user\folder\chromedrivermanager.exe"
browser = webdriver.Chrome(options=options)
You could try to change the parameters of webdriver.Chrome() as it asks for a single parameter as I did, and it worked.
Upvotes: 0
Reputation: 5902
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
Upvotes: 97
Reputation: 734
this seems to work for me
chrome_options = webdriver.ChromeOptions()
settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"selectedDestinationId": "Save as PDF",
"version": 2,
}
prefs = {'savefile.default_directory': '/Users/gregreynders/PycharmProjects/Webscraper'}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(chrome_options=chrome_options , executable_path="/Applications/chrome/chromedriver" )
driver.get("https://google.com")
driver.execute_script('window.print();')
driver.quit()
Upvotes: 1
Reputation: 420
# -*- 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()
Upvotes: 12
Reputation: 499
Note: Both .py file and Folder "PDF_Folder" in same location and file should download in Folder "PDF_Folder"
exepath = sys.arg[0]
# get the path from the .py file
Dir_path = os.path.dirname(os.path.abspath(exepath))
# get the path of "PDF_Folder" directory
Download_dir = Dir_path+"\\PDF_Folder\\"
preferences = {"download.default_directory": Download_dir , # pass the variable
"download.prompt_for_download": False,
"directory_upgrade": True,
"safebrowsing.enabled": True }
chrome_options.add_experimental_option("prefs", preferences)
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=r'/pathTo/chromedriver')
driver.get("urlfiletodownload");
Upvotes: 2
Reputation: 11122
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)
Upvotes: 5
Reputation: 41
For anybody still wondering why their implementation doesn't work: You have to put the FULL PATH for it to work. e.g. '/Users/you/dlfolder' won't work, while 'C:/Users/you/dlfolder' will.
Upvotes: 3
Reputation: 760
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)
Upvotes: 47
Reputation: 688
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": ""}}}
Upvotes: 8