Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

selenium - Auto download PDF in Firefox

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...