0

I am trying to bypass the firefox download manager window when downloading a pdf file in a selenium test by using firefox profile settings:

        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", "path/to/downloads");
        profile.setPreference("browser.download.useDownloadDir", true);
        profile.setPreference("browser.helperApps.neverAsk.openFile", "application/x-pdf, application/pdf, application/octet-stream, text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-pdf, application/pdf, application/octet-stream, text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);
        profile.setPreference("browser.download.manager.closeWhenDone", false);
        FirefoxOptions options = new FirefoxOptions();
        options.setProfile(profile);
        WebDriver driver = new FirefoxDriver(options);
        driver.get("https://mozilla.github.io/pdf.js/web/viewer.html");
        Thread.sleep(3000);
        driver.findElement(By.id("download")).click();
        Thread.sleep(5000);

I've tried different variations of profile preferences, but nothing seems to be working.

It's also important to note that I am seeing the file appear in the downloads folder as some random string with a weird extension, something like this: 5daYs8iE.pdf.part. If I exit the test without clicking the OK button on the download window then the file disappears, but if I click it, then the file saves and changes its extension to PDF

On org.seleniumhq.selenium:selenium-firefox-driver:3.141.59

Tyler Stutson
  • 43
  • 1
  • 8

1 Answers1

0

Instead of setting all the preferences you can manualy create a Firefox profile, set manualy all you need (PDF autodownload, etc.) and load the existing profile like this:

package tyler;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.ProfilesIni;

public class Tyler {

    public static void main(String[] args) throws InterruptedException {
        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\\pavel.burgr\\Desktop\\webdrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver(options);
        driver.manage().window().maximize();
        driver.get("https://mozilla.github.io/pdf.js/web/viewer.html");
        Thread.sleep(10000);
        driver.findElement(By.id("download")).click();
        Thread.sleep(10000);
    }

}
pburgr
  • 1,722
  • 1
  • 11
  • 26
  • could you elaborate on the use of setBinary and System.setProperty? im not sure that they are necessary in my case, as i am able to run my tests without them, and i can see from about:config that all the preferences that i have specified are actually set. still, however, it is not bypassing the download manager no matter what preferences i configure. i think im just missing that right combination of preferences, and or the correct mime type (which is strange to me, since it just appears to be a regular pdf, but you never know) – Tyler Stutson Oct 30 '19 at 13:45
  • were you able to bypass the download manager on the example pdf i provided? – Tyler Stutson Oct 30 '19 at 14:06
  • also, i guess it would have been helpful to mention that im on mac – Tyler Stutson Oct 30 '19 at 15:30
  • I just manualy run firefox with selenium_profile profile (created manualy as well) and set Content Type 'Portable document format' to value 'Save file'. In case of Mac set proper paths (2x .exe from the code above). – pburgr Oct 30 '19 at 15:45