Search code examples
javaseleniumfirefoxselenium-firefoxdriverfirefox-profile

Selenium webdriver (java) - file download dialog


I have following @BeforeClass:

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

    FirefoxProfile FF_profile = new FirefoxProfile();
    FF_profile.setPreference("browser.download.folderList",2);
    FF_profile.setPreference("browser.download.manager.showWhenStarting",false);
    FF_profile.setPreference("browser.download.dir","C:\\users\\pburgr\\downloads\\");
    FF_profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    FF_profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    FF_profile.setPreference("pdfjs.disabled", true);
}

But when downloading xlsx files, I'm stuck on the download dialog. What am I missing? thx


Solution

  • I moved options.setProfile(selenium_profile); behind setPreferences commands and it works

    Another approach is to create manualy new browser profile in firefox.exe -p. Customize the profile (automatic download xls files) and launch selenium this way:

    @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();}