我想使用 selenium 在 chrome 中下载 pdf。
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")
+ System.getProperty("file.separator")
+ "BrowserDrivers"
+ System.getProperty("file.separator")
+ "chromedriver.exe");
String downloadFilepath = "C:\\Users\\Vinod\\Downloads";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
//Save Chrome Opions
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);
driver.get(url);
我试过上面的代码,但它不工作
原文由 Vinod Kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议
由于 chrome 57 自动 pdf 预览不再作为插件工作,现在有一个设置可以更改以使其工作。您实际上可以通过检查 chrome 自己的首选项对话框来检查 pref 的名称,在“内容设置”下的标记为“在默认 PDF 查看器应用程序中打开 PDF 文件”。
您可以将其设置为 false 以避免自动 pdf 预览,如下所示(ruby 示例):