Reputation: 155
I can't set profile with automatic download for CSV file when using selenium.
Among the others, I tried solutions presented in the following questions:
How do I automatically download files from a pop up dialog using selenium-python
https://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver
On my "regular firefox" I can set the rule to download it automatically, it works, however when geckodriver runs firefox, this rule/profile does not apply.
I have administrator rights. And As I read on another question, my orange bar in firefox is a result of controlling it by webdriver.
Here is sample of my code:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "CSV File,text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "F:\\Delete")
driver=webdriver.Firefox(firefox_profile=profile)
1. Could you guys help me understand what am I mising?
2. How can I download CSV files automatically?
Upvotes: 0
Views: 1072
Reputation: 142631
I tested your code on some CSV
and I had to use application/octet-stream
because sometimes servers may send file with different type.
As I remeber application/octet-stream
was popular method on web pages to force downloading - especially for PDF
- because some users may have settings in browser which display PDF
with built-in viewer instead of downloading.
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")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "CSV File,text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "/home/furas/")
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()
EDIT: For other users: it needed also binary/octet-stream
Upvotes: 1
Reputation: 14135
I have been using the below logic and working for me in Java, you can easily port this to python.
FirefoxProfile profile = new FirefoxProfile();
// set the download folder directory
profile.setPreference("browser.download.dir", this.getDownloadFolderPath());
// the last folder specified for a download
profile.setPreference("browser.download.folderList", 2);
// hide Download Manager window when a download begins
profile.setPreference("browser.download.manager.showWhenStarting", false);
/**
This is the most important setting that will make sure the pdf is downloaded
without any prompt
*/
profile.setPreference("pdfjs.disabled", true);
profile.setPreference("pref.downloads.disable_button.edit_actions", false);
profile.setPreference("media.navigator.permission.disabled", true);
// A comma-separated list of MIME types to save to disk without asking what to
// use to open the file.
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/pdf,application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip,text/csv,text/plain,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;octet/stream");
// A comma-separated list of MIME types to open directly without asking for
// confirmation.
profile.setPreference("browser.helperApps.neverAsk.openFile",
"application/pdf,application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/zip,text/csv,text/plain,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;octet/stream");
// Do not ask what to do with an unknown MIME type
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
// Leave the window in the background when starting a download (Default Setting
// is false)
profile.setPreference("browser.download.manager.focusWhenStarting", false);
// popup window at bottom right corner of the screen will not appear once all
// downloads are finished.
profile.setPreference("browser.download.manager.showAlertOnComplete", true);
// Close the Download Manager when all downloads are complete
profile.setPreference("browser.download.manager.closeWhenDone", true);
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
Make sure to consume the options
while creating the driver instance.
And let me know if you are still facing the issue.
Upvotes: 1