FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
// Accept Untrusted Certificates
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
//Directly download PDF
profile.setPreference("browser.helperApps.neverAsk.openFile", "application/pdf");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
profile.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer
// Set Firefox profile to capabilities
options.setCapability(FirefoxDriver.PROFILE, profile);
我已经使用以上代码配置了firefox,以便当使用Selenium Webdriver单击PDF链接时,它可以直接下载PDF。当自动化脚本单击下载PDF的链接时,该脚本直接下载文件但是它失去了原始名称,但下载为'Document',请参阅附件以获取已下载文件的屏幕截图。
解决此问题的方法是http download file name-Web服务器需要将某些HTTP标头发送回客户端以设置文件名。您不会通过浏览器设置或Selenium来解决此问题。
解决此问题需要在服务器的响应中设置content-disposition
HTTP标头。像这样的东西:
content-disposition: attachment; filename=TheFileNameYouWant.pdf