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 = 'https://data.europa.eu/euodp/pl/data/dataset/covid-19-coronavirus-data' profile = webdriver.FirefoxProfile() profile.set_preference("browser.download.panel.shown", False) profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel,application/octet-stream,binary/octet-stream") profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "CSV File,text/csv,application/vnd.ms-excel") #profile.set_preference("browser.helperApps.alwaysAsk.force", False) profile.set_preference("browser.download.folderList", 2); profile.set_preference("browser.download.dir", "/home/furas/") # destination folder driver = webdriver.Firefox(firefox_profile=profile) driver.get(url) item = driver.find_element_by_xpath('//div[@id="dataset-resources"]//li[2]//a') print('text:', item.text) print('href:', item.get_attribute('href')) item.click()
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.
Notes:
Stackoverflow: Selenium Python - problem with downloading file, preferences are not working
If you like it
Buy a Coffee
