Downloading file to specified location with Selenium and python
Asked Answered
D

3

30

Ok so far i have my programing going to the website i want to download link from and selecting it, then the firefox dialogue box shows up and i don't know what to do. i want to save this file to a folder on my desktop. I am using this for a nightly build so i need this to work. Please help.

Here is my code that grabs the download link from the website:

driver = web driver.Firefox()
driver.implicitly_wait(5)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]".click()
Dubitable answered 11/8, 2014 at 20:11 Comment(0)
T
76

You need to make Firefox save this particular file type automatically.

This can be achieved by setting browser.helperApps.neverAsk.saveToDisk preference:

from selenium import webdriver

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

driver = webdriver.Firefox(firefox_profile=profile)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]").click()

More explanation:

  • browser.download.folderList tells it not to use default Downloads directory
  • browser.download.manager.showWhenStarting turns of showing download progress
  • browser.download.dir sets the directory for downloads
  • browser.helperApps.neverAsk.saveToDisk tells Firefox to automatically download the files of the selected mime-types

You can view all these preferences at about:config in the browser. There is also a very detailed documentation page available here: About:config entries.

Besides, instead of using xpath approach, I would use find_element_by_partial_link_text():

driver.find_element_by_partial_link_text("DEV.tgz").click()

Also see:

Talia answered 11/8, 2014 at 20:25 Comment(9)
I am getting an error on the 'profile = webdriver.FFP' line, IndentationError: unexpected indentDubitable
@JeradBill this is something to do with your particular code. Not the one I've provided.Talia
Any good idea on doing so in a WebDriver agnostic way? (e.g. Firefox and PhantomJS interchangeably)Cowpoke
Is it possible to set browser.helperApps.neverAsk.saveToDisk to all files? I don't know about the file type I download.Dunc
@Dunc Set browser.download.useDownloadDir to True.Roee
@Talia what is equivalent of option browser.helperApps.neverAsk.saveToDisk for chromdriver?Swabber
How to specify more than one mimetype in "browser.helperApps.neverAsk.saveToDisk"? By comma-separating the values?Martinet
How do I use this in IE?Barong
If you are trying to download files with no specified mimetype, e.g.: .dmp, you may use mimetype application/octet-streamCertified
L
3

@alecxe's answer updated - firefox_profile has been deprecated; this answer works as of March 2023, Firefox 110, Python 3.9, Selenium 4.8.2:

from selenium import webdriver
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", 'PATH TO DESKTOP')
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")

driver = webdriver.Firefox(options=options)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]").click()
Leigha answered 6/3, 2023 at 17:44 Comment(0)
R
2

If the application is generated dynamically (mime-types) using Chrome browser will be a better approach since the Chrome will not open the file download pop-up.But multiple download option should be enabled if you need multiple downloads.

Ribbonwood answered 30/3, 2015 at 12:2 Comment(3)
I am trying to do this same thing using Chrome, but I am having trouble setting the location of where to download the file. The same Profile functionality doesn't seem to work. Does anyone know how to set the download location in Chrome?Franfranc
chromePrefs.put("download.default_directory",downloadFilepath); _ChromeOptions.setExperimentalOption("prefs", chromePrefs);Kuster
@niNa how about browser.helperApps.neverAsk.saveToDisk ?Swabber

© 2022 - 2024 — McMap. All rights reserved.