5

我正在尝试使用 Selenium Web 驱动程序和 Java 下载 pdf 文件。大约 2 周前我最后一次运行它时运行良好,但现在每次点击 pdf 链接时,它都会打开 pdf 阅读器。

我在测试中创建的 firefox 配置文件没有更改,它设置了下载位置并将文件设置为自动下载(如果它们是 pdf 或 csv)。csv 文件仍然可以正常工作并下载到正确的文件夹。

在我的代码中,我将 pdfjs.disabled 设置为 true,如果我在 webdriver firefox 实例中打开 about:config,我可以看到它设置正确。

如果我在另一个 firefox 实例中将 pdfjs.disabled 设置为 true 并手动单击一个链接,它可以正常工作。

我不确定自上次运行测试以来是否已更新 Firefox,但我还在我的计算机上安装了 adobe reader。

请问谁能告诉我是什么让它突然停止工作?

这是我创建的配置文件以及我调用 webdriver 的方式。我正在使用最新版本的 Firefox 21.0。

FirefoxProfile firefoxProfile = new FirefoxProfile();

// Set profile to accept untrusted certificates
firefoxProfile.setAcceptUntrustedCertificates(true);

//Set profile to not assumet certificate issuer is untrusted
firefoxProfile.setAssumeUntrustedCertificateIssuer(false);

//Set download location and file types
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir",reportFolder);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/pdf,application/csv,application/vnd.ms-excel");

// Set to false so popup not displayed when download finished.
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete",false);

firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete",false);
firefoxProfile.setPreference("browser.download.manager.showWhenStartinge",false);
firefoxProfile.setPreference("browser.download.panel.shown",false);
firefoxProfile.setPreference("browser.download.useToolkitUI",true);

// Set this to true to disable the pdf opening
firefoxProfile.setPreference("pdfjs.disabled", true);

driver = new FirefoxDriver(firefoxProfile);

更新:我删除了 adobe reader,这又开始工作了。阅读器必须在配置文件中设置一些我需要禁用的内容才能使其与阅读器一起使用。有人有什么想法吗?

4

1 回答 1

1

尝试

firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf,application/vnd.adobe.xfdf,application/vnd.fdf,application/vnd.adobe.xdp+xml");

诀窍是将 PDF MIME 添加到plugin.disable_full_page_plugin_for_types首选项。

这适用于 Firefox 26。

于 2014-02-03T20:41:57.923 回答