在Firefox中自动下载PDF

在Firefox中自动下载PDF,firefox,selenium,selenium-webdriver,Firefox,Selenium,Selenium Webdriver,我希望Firefox直接下载PDF文件,而不是在浏览器中显示它们。我使用了以下设置 FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.setPreference("browser.download.folderList", 2); firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false); firefoxP

我希望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

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

它应该是“保存文件”

Firefox仍然在浏览器中显示PDF。我正在使用Firefox 29.0.1,首选项值是否已更改

这对我很有用:

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


希望有帮助

您的屏幕截图显示Firefox将预览
pdf
文件,但您的Firefox仍会弹出“另存为”对话框,这对我来说毫无意义

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

另外,请确保您没有安装任何第三方Firefox PDF查看插件。 如果您的计算机上安装了Adobe Reader,那么它会将Acrobat设置为Firefox中的PDF查看器。类似地,我以前在我的电脑上也有,它会覆盖Firefox设置以预览PDF,无论
about:config
中有什么内容

FirefoxProfile FirefoxProfile=新的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);
//使用此选项可禁用用于在Firefox中预览PDF的Acrobat插件(如果您的计算机上安装了Adobe reader)
setPreference(“plugin.scan.Acrobat”,“99.0”);
setPreference(“plugin.scan.plid.all”,false);
WebDriver驱动程序=新的FirefoxDriver(firefoxProfile);
//这只是一个示例URL
驱动程序。获取(“http://www.energy.umich.edu/sites/default/files/pdf-sample.pdf");
进一步阅读:

  • 我以前回答过类似的问题:
  • 关于Firefox中插件扫描的文章:

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

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)

希望这能有所帮助。

对我来说,这两种方法都奏效了

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


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

@Yi Zeng给出的设置非常好,但不起作用。 在打开Firebfox浏览器之后,由于selenium版本中的一个错误,首选项没有得到应用。 因此,如果您面临与此处所述相同的问题,那么您需要像这样做以应用代码设置的首选项:

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

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

对于使用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");

1.以前的Firefox版本是否适用?2.什么是
PDF\u URL
?我遇到过这样的情况,它只适用于某些PDF(即使MIME类型相同)。您可能想尝试其他一些pdf文件(可公开访问的文件,以便我们可以复制)。如果它适用于某些PDF,则表示您的代码是正确的。@YiZeng第一次这样做。无法共享特定于公司的URL。我也试过使用“”,但实际上它没有显示另存为对话框,而是在浏览器中显示PDF。我的错。。也尝试了你的解决方案,但没有luck@Karna当前位置我测试了它,为我工作,所以我打赌您的环境中的其他东西是错误的。您是否安装了Firefox PDF插件?这可能是我在回答中解释的问题。另外,使用上面的代码,当您手动打开“选项”对话框时,您会看到什么?默认的行为是什么?没有任何额外的插件,但是Firefox使用的是Adobe插件(我想这是默认的)。在“选项”中,默认值也是“预览”Firefox@Karna:我想这可能就是问题所在。这不是Firefox的默认设置,而是“第三方”。如果您在关于:插件的
中看到Adobe插件,请卸载它。听起来不是个好主意。1.因为其他测试如果想在浏览器中打开PDF,可能需要使用此插件。2.没有简单的方法删除adoble插件,需要手动删除它。通过设置首选项难道不可能吗?对于这个问题,我查阅了大量不同的答案,并添加了pdfjs.disabled首选项。谢谢