How to change download directory location path in Selenium using Chrome?

Viewed 1822

I'm using Selenium in Python and I'm trying to change the download path. But either this:

prefs = {"download.default_directory": "C:\\Users\\personal\\Downloads\\exports"}
options.add_experimental_option("prefs", prefs)`

or this

options.add_argument("--download.default_directory --C:\\Users\\personal\\Downloads\exports")`

are not working.

In first case I also get the error

from invalid argument: unrecognized chrome option: prefs

Can someone help?

3 Answers
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
prefs = {"profile.default_content_settings.popups": 0,    
        "download.default_directory":r"C:\Users\xxxx\xxxx\ccc\xxxx\xx\xx", ### Set the path accordingly
        "download.prompt_for_download": False, ## change the downpath accordingly
        "download.directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
driver = Chrome(service=Service(PATH), options=options)

To change the download directory/path you can use the following code block:

compatible code

from selenium.webdriver.chrome.options import Options

options = Options()
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)

References

You can find a couple of relevant detailed discussions in:

After trying unlimited solutions on the internet, here is what works for me to set download path in Python Selenium Chrome.

from selenium.webdriver import Chrome, ChromeOptions

prefs = {
    "download.default_directory": "/Users/your_user/Desktop",
    "download.directory_upgrade": True,
    "download.prompt_for_download": False,
}

chromeOptions = ChromeOptions()
chromeOptions.add_experimental_option("prefs", prefs)
driver = Chrome(options=chromeOptions)
Related