10

I want Firefox to directly download the PDF files instead of showing them in browser. I used following settings

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", "c:\\tmp");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
WebDriver driver = new FirefoxDriver(firefoxProfile);
// Its just a sample URL 
driver.get("http://www.energy.umich.edu/sites/default/files/pdf-sample.pdf");

On about:config page I can see that this setting are successfully reflected also the response type is application/pdf.

enter image description here

When Webdriver launches Firefox I can see following option.

enter image description here

It should be "Save File".

Still Firefox is showing PDF in browser. I am using Firefox 29.0.1, does the preference values have changed?

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • 1. Did it work for previous Firefox versions before? 2. What is the `PDF_URL`? I have met situations that it only works for some PDFs (even though MIME types are identical). You might want to try some other pdf files (publicly accessible ones, so that we can reproduce). If it works for some pdfs, then it means your code is correct. – Yi Zeng May 22 '14 at 07:27
  • @YiZeng Doing it for the first time. Cant share URL as its company specific. I also tried with "http://www.energy.umich.edu/sites/default/files/pdf-sample.pdf" but no luck – Ajinkya May 22 '14 at 07:30

7 Answers7

16

It doesn't make sense to me that your screenshot shows Firefox will preview pdf files, but your Firefox still pops up "Save as" dialog.

Anyway, in order to make Firefox saving pdf files to a pre-defined folder as the default behaviour, you might want to try the following code, where setting pdfjs.disabled to true will prevent Firefox previewing the files.

Also, please ensure you don't have any third party Firefox PDF viewing plugins installed. If you have Adobe Reader installed on your computer, then it sets Acrobat as the PDF viewer inside Firefox. Similarly, I used to have Sumatra PDF Firefox plugin on my computer, it overrides Firefox settings to preview PDFs no matter what's in about:config.

FirefoxProfile firefoxProfile = new FirefoxProfile();

firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", "c:\\tmp");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

firefoxProfile.setPreference("pdfjs.disabled", true);

// Use this to disable Acrobat plugin for previewing PDFs in Firefox (if you have Adobe reader installed on your computer)
firefoxProfile.setPreference("plugin.scan.Acrobat", "99.0");
firefoxProfile.setPreference("plugin.scan.plid.all", false);

WebDriver driver = new FirefoxDriver(firefoxProfile);

// Its just a sample URL 
driver.get("http://www.energy.umich.edu/sites/default/files/pdf-sample.pdf");

Further reading:

Community
  • 1
  • 1
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • Actually its not showing Save as dialog, its showing PDF in browser. My bad.. Also tried your solution but no luck – Ajinkya May 22 '14 at 08:45
  • @Karna: I tested it, working for me, so I bet something else in your environment is wrong. Do you have any kind of Firefox PDF plugins installed? It might be the issue as I explained in the answer. Also, using my code above, what do you see when you manually open up "Options" dialog? What's the default behaviour there? – Yi Zeng May 22 '14 at 09:07
  • Dont have any extra plugin but Firefox is using Adobe plugin (Which is default I guess). Also in options default is Preview in Firefox – Ajinkya May 22 '14 at 09:11
  • @Karna: I guess that could be the problem. It's not the Firefox default, but "third party". If you see Adobe plugin in your `about:plugins`, please uninstall it. – Yi Zeng May 22 '14 at 09:14
  • Dont sound like a good idea. 1. As other tests might need to use this plugin if they want to open PDF in Browser. 2. There is no easy way to remove adoble plugin, need to manually delete it. Isn't it possible through setting preferences? – Ajinkya May 22 '14 at 09:22
  • DO you have Adobe plugin in your browser? How PDFs are showed if you choose Preview in firefox option? – Ajinkya May 22 '14 at 09:23
  • @Karna: I know some people might still need it. What you need to do now is to uninstall Adobe reader from your computer and see if this is the issue causing the problem. If so, then you need to make a decision what to do next. If not, install it back on. Adobe plugin is installed if you have Adobe reader on your computer, which I don't. Firefox has a built-in one, which doesn't need third party ones at all. https://support.mozilla.org/en-US/kb/view-pdf-files-firefox-without-downloading-them – Yi Zeng May 22 '14 at 09:25
  • I disabled Acrobat plugin but cant disable default firefox plugin (Adobe reader) – Ajinkya May 22 '14 at 09:32
  • @Karna: Correct, you might need to ***uninstall Adobe reader from your computer***. This is why I always recommend using a separate testing environment for Selenium related stuff. More details here in ["Use the Adobe Reader plugin to view or download PDF files"](https://support.mozilla.org/en-US/kb/use-adobe-reader-plugin-view-or-download-pdf-files), I quote "(Adobe Reader) It includes an Adobe Acrobat browser plugin for displaying PDF documents within Firefox and other Web browsers. (By default, Firefox uses its built-in PDF Viewer.)". – Yi Zeng May 22 '14 at 09:37
  • @Karna: I found a way to disable plugins when starting Firefox, therefore you might not to need uninstalling Adobe Reader anymore. Answer updated. – Yi Zeng May 22 '14 at 21:36
  • Great. Will try and let yiu know – Ajinkya May 23 '14 at 02:50
3

You can disable plugin while setting preference. This works for me

profile = webdriver.FirefoxProfile()

profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.dir", os.getcwd())

//below line was missing in yours

profile.set_preference("plugin.disable_full_page_plugin_for_types", "application/pdf")
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")  
driver = webdriver.Firefox(firefox_profile=profile)

Hope this helps.

vinu
  • 51
  • 2
  • I looked through a bunch of different answers for this issue and adding the pdfjs.disabled preference did it for me. Thanks! – carlin.scott Sep 25 '18 at 20:29
2

This works for me:

    WebDriver driver;

    FirefoxProfile fxProfile = new FirefoxProfile();
    fxProfile.setPreference("browser.download.folderList", 2);
    fxProfile.setPreference("browser.download.manager.showWhenStarting", false);
    fxProfile.setPreference("browser.download.dir",System.getProperty("java.io.tmpdir"));
    fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

    //You miss this line
    fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);

    driver = new FirefoxDriver(firefoxProfile);

Give a try.

Hope helps!

Morvader
  • 2,317
  • 3
  • 31
  • 44
2

For Firefox Quantum 57.0 64-bit, Selenium 3.8.1, the following solution works.

FirefoxProfile ffprofile = new FirefoxProfile();        

// Required if you want to download other than the default location
ffprofile.setPreference("browser.download.folderList", 2);
// Specify your own location
ffprofile.setPreference("browser.download.dir", "C:\\TestAutomationDataSheets\\Files_To_Download\\");
ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
ffprofile.setPreference("pdfjs.enabledCache.state", false);

DesiredCapabilities ffcapabilities = DesiredCapabilities.firefox();
ffcapabilities.setCapability(FirefoxDriver.PROFILE, ffprofile);

WebDriver driver = new FirefoxDriver(ffcapabilities);
t j
  • 7,026
  • 12
  • 46
  • 66
1

The settings given by @Yi Zeng is perfectly fine but that doesnt work out. As after opening the Firebfox brower the preferences not getting applied due to one bug in selenium version. So if you are facing the same issue as mentioned here https://github.com/seleniumhq/selenium/issues/3498 then you need to do the work around like this to apply the preferences set by the code:

    DesiredCapabilities dc = DesiredCapabilities.firefox();
    dc = DesiredCapabilities.firefox();
    dc.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
Aamir
  • 66
  • 6
1

The same as above for a remote Firefox Webdriver using Python Selenium:

from selenium import webdriver

from selenium.webdriver.firefox.options import Options

options = Options()
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/data");
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
profile.set_preference("pdfjs.disabled", True)
profile.set_preference("plugin.scan.Acrobat", "99.0")
profile.set_preference("plugin.scan.plid.all", False)

driver = webdriver.Remote(
    browser_profile=profile,
    command_executor='http://localhost:4444/wd/hub',
    desired_capabilities=options.to_capabilities()
)
driver.get("https://www.ti.com/lit/ds/symlink/sa555.pdf");
Alex44
  • 3,597
  • 7
  • 39
  • 56
0

for me just these two worked.

firefoxProfile.setPreference("pdfjs.disabled", true);   


firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
driver = new FirefoxDriver(firefoxProfile);
Neeraj
  • 1,163
  • 2
  • 18
  • 32