[英]How to download a pdf file in chrome using 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);
我尝试了上面的代码,但它不起作用
由于 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
)
您可以加载此页面并使用 selenium 导航更改设置:
chrome://settings/content/pdfDocuments
以下是任何使用 .NET 的人的 C# 选项
var tsTimeout = new TimeSpan(0, 5, 0);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", _downloadFolder);
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
chromeOptions.AddUserProfilePreference("download.directory_upgrade", true);
chromeOptions.AddUserProfilePreference("plugins.plugins_disabled", "Chrome PDF Viewer");
chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
_driver = new ChromeDriver(CWebCrawler.WebCrawlerRootFolder, chromeOptions, tsTimeout);
您需要将以下语句添加到您的 chrome 配置文件中:
chromePrefs.put("pdfjs.disabled", true);
似乎较新版本的浏览器具有在浏览器中显示 PDF 文件的内置功能。 有关更多信息,请参阅此信息,尽管它适用于 Firefox 配置文件,但仍然值得一读。
希望能解决您的问题,否则您可能需要降级 Chrome 以使其正常工作。 如果您有任何疑问,请告诉我。
您是否使用 Adobe Acrobat/Adobe Reader 来显示 PDF? 如果是这样,则可能是您需要修改 Abode 的行为。
以下是我必须采取的步骤:
您还可以在 Chrome 中禁用 Adobe 插件,这将强制 Chrome 下载 PDF。
它在 chrome://plugins 下。
但正如你所说的,它最新的 chrome 浏览器然后检查插件中是否可以看到 chrome PDF 视图,如果是的话也禁用它。
以下 python 代码在 Chrome 中为我工作,通过禁用 pdf 查看器来下载 pdf。
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": chromeDownloadPath,
"download.prompt_for_download": False,
"download.extensions_to_open": "applications/pdf",
"plugins.plugins_disabled": "Chrome PDF Viewer",
"plugins.always_open_pdf_externally": True}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('chromedriver', options=options)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.