Search on blog:

Selenium: How to download file to selected folder without asking for confirmation

On web pages adding application/octet-stream was popular method to force downloading - especially for PDF - because some users may have settings in browser which displays PDF with built-in viewer instead of downloading.

In this example for some computers it needed also binary/octet-stream

Minimal working code with example CSV

from selenium import webdriver

url = ''

profile = webdriver.FirefoxProfile()

profile.set_preference("", False)

profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "CSV File,text/csv,application/")
#profile.set_preference("browser.helperApps.alwaysAsk.force", False)

profile.set_preference("", 2);
profile.set_preference("", "/home/furas/")   # destination folder

driver = webdriver.Firefox(firefox_profile=profile)


item = driver.find_element_by_xpath('//div[@id="dataset-resources"]//li[2]//a')
print('text:', item.text)
print('href:', item.get_attribute('href'))

Sometimes it is good to use DevTools (tab: Network) in Firefox/Chrome to see request send to browser when file is downloaded to checks header Content-Type. It may use different value.

If you like it
Buy a Coffee