1

I want to download a file using selenium and geckodriver. By default, the browser displays a message asking the user to specify whether to open the file or to save it. From my understanding, there is no way to control this dialog from selenium. The key is to prevent the dialog from coming up in the first place and save the file directly. I can't find a way to do this.

I found a few related questions that were solved by setting the config options in geckodriver using selenium as follows:

profile.setPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

I manually verified the MIME type was correct with the javax.activation.MimetypesFileTypeMap library. Being a developer, I also tried every other type I could think of that might work (text/csv, text/plain, application/vnd.ms-excel). I then verified that the config settings were adjusted correctly by going to about:config in the browser instance created by my code, which they were. I tried all the code on this question, but that didn't work either. The differences I could find between my problem and this one were that 1. I'm downloading a csv instead of a pdf, and 2. the constructor FirefoxDriver(FireFoxProfile) doesn't exist for whatever version I'm using, which I downloaded after the question asked. I can't find documentation to make sure that I'm using FirefoxOptions correctly, but I think I am considering the config settings are set correctly. They just don't see to work correctly. I also tried using

options.addPreference(...)

instead of

profile.setPreference(...)

and no dice. Here is all the relevant code to creating the browser instance. The download is initialized by clicking on a link.

System.setProperty("filepath\\to\\geckodriver.exe");

//use this to turn off the insane amount of useless debug info
System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "null");

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);         
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.dir", "c:\\downloads");
profile.setPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
options.setProfile(profile);
b = new FirefoxDriver(options);

This resulted in the config settings correctly set, but the download dialog still shows when I click the link to download the file.

sketrik
  • 51
  • 4

0 Answers0