Geckodriver Selenium Auto下载PDF

时间:2018-08-16 17:25:35

标签: selenium geckodriver

我正在尝试在geckodriver / Firefox中自动下载.pdf文件。我搜索了stackoverflow和其他资源,并认为下面的代码应该可以工作:

profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'C:\\Users\\xyz\\Downloads\\')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf")
profile.set_preference("pref.downloads.disable_button.edit_actions", True)
profile.set_preference("browser.helperApps.neverAsk.openFile", "application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf")
browser = webdriver.Firefox(firefox_profile=profile)

我也尝试过:

profile.set_preference("pdfjs.disabled", True)

但是,Firefox不会自动下载.pdf(即使它是http请求中的application / pdf)。另外,在Firefox中加载该配置文件后,在“选项/应用程序”下,PDF格式仍显示“在Firefox中预览”,而不是“保存文件” ...我在做什么错?

2 个答案:

答案 0 :(得分:1)

尝试在现有列表中添加以下首选项:

fp.setPreference("pdfjs.enabledCache.state", false);
fp.setPreference("browser.helperApps.neverAsk.openFile","application/pdf");

答案 1 :(得分:0)

要禁用在Firefox中打开和下载pdf:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.download.dir", pathToDownload);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.addPreference("pdfjs.enabledCache.state",false); 
WebDriver driver = new FirefoxDriver(options);