3

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Luigi Montaleone
  • 53
  • 1
  • 1
  • 4
  • Does this answer your question? [Downloading a file at a specified location through python and selenium using Chrome driver](https://stackoverflow.com/questions/35331854/downloading-a-file-at-a-specified-location-through-python-and-selenium-using-chr) – JAdel Apr 02 '22 at 09:49
  • if you have a https://stackoverflow.com/help/minimal-reproducible-example then this would help users answer in more detail. – D.L Apr 02 '22 at 10:41

3 Answers3

1
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)
raviteja k
  • 94
  • 1
  • 1
  • 6
0

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

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)
B. Okba
  • 1,021
  • 12
  • 16