gpt4 book ai didi

java - 如何使用 selenium webdriver 在 chrome 中下载 pdf 文件

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:08:18 25 4
gpt4 key购买 nike

我想使用 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);

我试过上面的代码,但它不起作用

最佳答案

自 chrome 57 以来,自动 pdf 预览不再作为插件工作,现在您可以更改设置以使其工作。您实际上可以通过检查 chrome 自己的首选项对话框来检查 pref 的名称,在“内容设置”下的标记为“在默认 PDF 查看器应用程序中打开 PDF 文件”。 automatic pdf open pref

您可以将其设置为 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
)

关于java - 如何使用 selenium webdriver 在 chrome 中下载 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31672897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com