4

I'm try to setup Firefox in order to be auto-download files. I did how suggested in enter link description here, But I cannot get it to work.

This is my code:

FirefoxOptions options = new FirefoxOptions();
        options.SetPreference("browser.download.folderList", 2);
        options.SetPreference("browser.download.dir", "C:\\Windows\\temp");
        options.SetPreference("browser.download.useDownloadDir", true);
        options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
        options.SetPreference("pdfjs.disabled", true);  // disable the built-in PDF viewer
        options.SetPreference("browser.download.useDownloadDir", true);
        driver = new FirefoxDriver(options);
        driver.Manage().Window.Maximize();
        driver.Navigate().GoToUrl("https://www.mozilla.org/en-US/foundation/documents");
        driver.FindElement(By.LinkText("IRS Form 872-C")).Click();

The PDF is still opened in the browser PDF viewer.

bad_coder
  • 11,289
  • 20
  • 44
  • 72

2 Answers2

1

To disable open and download pdf in firefox:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.download.dir", downloadPath);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.addPreference("pdfjs.enabledCache.state",false); 
WebDriver driver = new FirefoxDriver(options);

List of Mime Tipes can be found here.

Sers
  • 12,047
  • 2
  • 12
  • 31
  • options.addPreference("pdfjs.enabledCache.state",false); was the one missing. Thanks – Roberto Raccosta Aug 27 '18 at 14:05
  • I am constructing the download path dynamically specific to my test, and that path does not exist. For chrome, it creates folder hierarchy to create download path and downloads the file there. But for Firefox, it does not create a path there. Are there any settings available in Firefox browser so that it creates a download path if it does not exist and download there ? – CSharp Oct 05 '21 at 22:08
-1

My mistake, I didn't realize that the question was for C #.

So I did it in Java, I suppose selenium functionalities are very similar regardless of language. However, the important thing here is how to configure FirefoxDriver.

Using selenium 3.8:

    FirefoxProfile profile = new FirefoxProfile();
    //if you want to download the file to a different directory than the default
    profile.setPreference("browser.download.dir", "dirPath");

    //0: the desktop, 1 (default): the downloads folder, 2: the last folder specified for a download
    profile.setPreference("browser.download.folderList", 2);

    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf;");
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("pdfjs.disabled", true);

    //The previous configuration can also be done in FirefoxOptions, I did not know and I simply passed the FirefoxProfile object
    FirefoxOptions fo = new FirefoxOptions();
    fo.setProfile(profile);

    FirefoxDriver driver = new FirefoxDriver(fo);

    driver.get("http://your.web");
    driver.findElement(By.id("download_button")).click();
eniel.rod
  • 855
  • 8
  • 12