13

I know this question has been asked before...but I've tried multiple approaches and for some reason anything I download from the driver keeps going to my Downloads folder.

Basically I navigate to a website and download something by clicking a download link like so:

result.click()

This downloads the file fine. But I want to download it to a specific directory. I've tried doing these approaches to change the download directory:

driver = webdriver.Firefox()
profile = webdriver.FirefoxProfile()

driver.command_executor._commands["SET_CONTEXT"] = ("POST", "/session/$sessionId/moz/context")
driver.execute("SET_CONTEXT", {"context": "chrome"})
driver.execute_script("""
  Services.prefs.setBoolPref('browser.download.useDownloadDir', true);
  Services.prefs.setStringPref('browser.download.dir', arguments[0]);
  """, directory)

driver.execute("SET_CONTEXT", {"context": "content"})

and

profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", directory)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")

Where directory is my desired location.

Neither of these worked...can anyone explain why or show me how to actually achieve this?

Thanks

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Derek Eden
  • 4,403
  • 3
  • 18
  • 31
  • Make sure you're initializing the driver with the correct profile, and make sure that your directory is valid. Also, try using forward slashes instead of backslashes – Kasem Alsharaa Feb 11 '20 at 13:53
  • how do I initialize with a profile? My code looks like this: `driver = webdriver.Firefox()` `driver.get(url)` `profile = webdriver.FirefoxProfile()` – Derek Eden Feb 11 '20 at 14:16
  • and does initializing with a profile allow me to change the download directory for that profile over and over without having to re-initialize? – Derek Eden Feb 11 '20 at 14:17
  • _how do I initialize with a profile?_ https://stackoverflow.com/questions/50321278/how-to-load-firefox-profile-with-python-selenium – AMC Feb 11 '20 at 20:14
  • Does this answer your question? [Downloading file to specified location with Selenium and python](https://stackoverflow.com/questions/25251583/downloading-file-to-specified-location-with-selenium-and-python) – AMC Feb 11 '20 at 20:15

3 Answers3

18

As of 2021 the FirefoxProfile class used in earlier answers is deprecated, for the new selenium.webdriver.firefox.options.Options:

from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", "./downloads")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")

driver = webdriver.Firefox(options=options)
leo
  • 8,106
  • 7
  • 48
  • 80
4

You're initializing your browser with the default profile since you're not passing any profile argument to your webdriver.Firefox()

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", directory)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")

driver = webdriver.Firefox(profile)
Kasem Alsharaa
  • 892
  • 1
  • 6
  • 15
  • ah...this is what I was looking for...my bad it was so obvious - first time using selenium...I'll give this a whirl when I get a minute but looks liek it'll work..thanks – Derek Eden Feb 11 '20 at 14:21
  • 2
    will this allow me to change the directory multiple times once the driver is initialized? – Derek Eden Feb 11 '20 at 14:21
  • 2
    This wont allow you to change preferences during runtime. You would have to do it via about:config. Check this example https://tarunlalwani.com/post/change-profile-settings-at-runtime-firefox-selenium/ – Kasem Alsharaa Feb 11 '20 at 14:37
  • 1
    you can not change preferences during runtime.it can only be initialised once when the driver created. – Manali Kagathara Feb 11 '20 at 14:38
  • _will this allow me to change the directory multiple times once the driver is initialized?_ @DerekEden What do you need to do that for? Is there any way around it? – AMC Feb 11 '20 at 20:15
  • @AMC I have a base/parent url, go to each link in that url, press a download button, go back up one level, go to next link, download next...etc... and I want each download to save in a different folder...I would prefer to dynamically switch directories so I don't have to initialize a new driver every time – Derek Eden Feb 11 '20 at 20:30
  • @DerekEden Could you download all the files in the same place, and then move them to the appropriate directory? – AMC Feb 12 '20 at 05:05
3

below options, it's working fine for me.

# set download options
download_path = DOWNLOADS_PATH

# 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory
firefox_options.set_preference("browser.download.folderList", 2)
firefox_options.set_preference("browser.download.dir", download_path)
Manali Kagathara
  • 743
  • 4
  • 11