在Firefox中自动下载PDF

时间:2014-05-22 07:18:29

标签: firefox selenium selenium-webdriver

我希望Firefox直接下载PDF文件而不是在浏览器中显示它们。我使用了以下设置

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");

about:config页面上,我可以看到此设置已成功反映,响应类型为application/pdf

enter image description here

当Webdriver启动Firefox时,我可以看到以下选项。

enter image description here

应该是“保存文件”。

Firefox仍在浏览器中显示PDF。我使用的是Firefox 29.0.1,偏好值是否已更改?

7 个答案:

答案 0 :(得分:11)

我觉得你的截图显示Firefox会预览pdf个文件,但你的Firefox仍会弹出“另存为”对话框。

无论如何,为了使Firefox将pdf文件保存到预定义文件夹作为默认行为,您可能需要尝试以下代码,将pdfjs.disabled设置为true将阻止Firefox预览文件。

另外,请确保您没有安装任何第三方Firefox PDF查看插件。 如果您的计算机上安装了Adobe Reader,则会将Acrobat设置为Firefox中的PDF查看器。同样,我以前在我的计算机上有Sumatra PDF Firefox plugin,它会覆盖Firefox设置以预览PDF,无论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");

进一步阅读:

答案 1 :(得分:2)

这对我有用:

    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);

试一试。

希望有所帮助!

答案 2 :(得分:2)

您可以在设置首选项时禁用插件。这对我有用

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)

希望这有帮助。

答案 3 :(得分:2)

对于Firefox Quantum 57.0 64位,Selenium 3.8.1,以下解决方案有效。

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);

答案 4 :(得分:1)

@Yi Zeng给出的设置非常好但是没有用。 在打开Firebfox浏览器后,由于selenium版本中的一个错误,首选项未被应用。 因此,如果您遇到与https://github.com/seleniumhq/selenium/issues/3498中提到的相同的问题,那么您需要像这样处理以应用代码设置的首选项:

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

答案 5 :(得分:0)

对我来说,只有这两个工作。

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


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

答案 6 :(得分:0)

与上面使用Python Selenium的远程Firefox Webdriver相同:

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");