[ACCEPTED]-Auto download PDF in Firefox-selenium-webdriver

Accepted answer
Score: 15

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

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

Also, please ensure you don't 7 have any third party Firefox PDF viewing 6 plugins installed. If you have Adobe Reader 5 installed on your computer, then it sets 4 Acrobat as the PDF viewer inside Firefox. Similarly, I 3 used to have Sumatra PDF Firefox plugin on my computer, it overrides 2 Firefox settings to preview PDFs no matter 1 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:

Score: 3

You can disable plugin while setting preference. This 1 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.

Score: 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!

0

Score: 2

For Firefox Quantum 57.0 64-bit, Selenium 1 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);
Score: 1

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

    DesiredCapabilities dc = DesiredCapabilities.firefox();
    dc = DesiredCapabilities.firefox();
    dc.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
Score: 1

The same as above for a remote Firefox Webdriver 1 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");
Score: 0

for me just these two worked.

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


firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
driver = new FirefoxDriver(firefoxProfile);

0

More Related questions