Cannot auto-save file with Selenium Firefox
I'm using Selenium to click a link in Firefox and I want it to download the file automatically. I have looked at various resources on the internet inc. Selenium firefox profile for saving a file. & Set Firefox profile to download files automatically using Selenium and Java. But I cannot get it to work. It just keep giving me a prompt with an open/save dialog. I am using Firefox 57.0.2, firefox driver 3.8.1, Geckodriver 0.19.1. I can manually set the firefox options to save the file but within the code i create a new profile so this obviously gets ignored. My code is:
System.setProperty(webdriver.gecko.driver",System.getProperty("user.dir") + Constants.GECKODRIVER);
FirefoxOptions firefoxOptions = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", dirPath);
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.helperApps.neverAsk.openFile","application/pdf,text/plain,application/octet-stream,application/x-pdf,application/vnd.pdf,application/vnd.openxmlformats-officedocument.spreadsheethtml,text/csv,text/html,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf,text/plain,application/octet-stream,application/x-pdf,application/vnd.pdf,application/vnd.openxmlformats-officedocument.spreadsheethtml,text/csv,text/html,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel");
profile.setPreference("browser.helperApps.alwaysAsk.force, false);
profile.setPreference("browser.download.manager.useWindow, false);
profile.setPreference("browser.download.manager.focusWhenStarting, false);
profile.setPreference("browser.download.manager.alertOnEXEOpen, false);
profile.setPreference("browser.download.manager.showAlertOnComplete, false);
profile.setPreference("browser.download.manager.closeWhenDone, false);
profile.setPreference("browser.allowpopups, false);
profile.setPreference("pdfjs.disabled",true);
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificatesIssuer(true);
firefoxOptions.setProfile(profile);
currentdriver = new FirefoxDriver();
I've even tried
profile.setPreference("plugin.disable_full_page_plugin_for_types","application/pdf,application/x-pdf");
profile.setPreference("plugin.scan.Acrobat,"99.0");
I have tried looking at the MIME types of the 2 files I need (.pdf and .xlsx) and I think they are text/html;charset=iso-8859-1.
Suggestions please?
I too had no success using set_preference in webdriver.FirefoxProfile from selenium. What worked was a modification to the handlers.json file in the firefox profile directory.
The following had to be inserted under mimeTypes in the json file :
"application/text":{"action":0,"extensions":["csv"]},