Vin*_*mar 4 java selenium selenium-webdriver
我想使用 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);
Run Code Online (Sandbox Code Playgroud)
我尝试了上面的代码,但它不起作用
由于 chrome 57 自动 pdf 预览不再作为插件工作,现在您可以更改一个设置来使其工作。实际上,您可以通过检查 chrome 自己的首选项对话框来检查首选项的名称,在“内容设置”下,显示“在默认 PDF 查看器应用程序中打开 PDF 文件”的标志。

您可以将其设置为 false 以避免自动 pdf 预览,如下所示(ruby 示例):
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
'args' => ['disable-gpu', "--window-size=1920,1080"],
prefs: {
"download.prompt_for_download": false,
"download.directory_upgrade": true,
"plugins.always_open_pdf_externally": true,
"download.default_directory": DownloadHelpers::PATH.to_s
}
}
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39789 次 |
| 最近记录: |