0

I'm trying to set up a Firefox(Windows, 61.0) profile for use in Selenium WebDriver (3.13.0) with Java so that Firefox automatically downloads files so that I can bypass download dialogs.

Here's the code:

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.useDownloadDir",true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/x-download");
return options.setProfile(profile);

browser.download.folderList and browser.helperApps.neverAsk.saveToDisk both affect the settings you'd expect them to affect, but in the case of browser.download.useDownloadDir, it doesn't affect the actual setting (i.e. it remains false). Rather, it creates a new, similar (?) setting called services.sync.prefs.sync.browser.download.useDownloadDir.

enter image description here

Any idea what the issue is here, and how I can set the useDownloadDir setting to true?

g_raham
  • 55
  • 6

2 Answers2

0

OPTION 1: Specify the mime type of downloaded files. This is an example for XLS / XLSX files:

FirefoxProfile selenium_profile = new FirefoxProfile();
selenium_profile.setPreference("browser.download.folderList",2);
selenium_profile.setPreference("browser.download.dir", "C:\\Users\\pburgr\\Desktop\\BP_usr_tmp\\");
selenium_profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
options.setProfile(selenium_profile);

OPTION 2: Use an existing Firefox profile. With existing profile "selenium_profile" I use this:

@BeforeClass
    public static void setUpClass() {
        FirefoxOptions options = new FirefoxOptions();
        ProfilesIni allProfiles = new ProfilesIni();         
        FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
        options.setProfile(selenium_profile);
        options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
        driver = new FirefoxDriver(options);
        driver.manage().window().maximize();
        }

Use Firefox profile manager (Win+R: firefox -p) to create a new profile. Run Firefox in the new profile and set up customizations you need including autodownload for specific file types.

pburgr
  • 1,722
  • 1
  • 11
  • 26
0

I was able to solve this by setting those prefs during runtime as described in this post:

Selenium firefox profile update download directory after creating webdriver

My issue is likely caused by the enterprise settings on my machine, which could force prefs to default a certain way each time a new window opens regardless of what's passed to the browser via Selenium. For example, I wasn't able to edit browser.download.useDownloadDir, browser.download.dir, and browser.download.folderList (among others). My mime types were set correctly and the issue still occurred on my end.