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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31672897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:24:04  来源:igfitidea点击:

How to download a pdf file in chrome using selenium webdriver

javaseleniumselenium-webdriver

提问by Vinod Kumar

I want to download pdf in chrome using selenium.

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

I tried above code but its not working

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

回答by fahad

Are you using Adobe Acrobat/Adobe Reader to display the PDFs? If so, it is probably Abode's behavior you need to modify.

您是否使用 Adob​​e Acrobat/Adobe Reader 来显示 PDF?如果是这样,则可能是您需要修改 Abode 的行为。

Here are the steps I had to take:

以下是我必须采取的步骤:

  1. Open Adobe Reader
  2. Edit menu, Preferences
  3. Selcet Internet from the Categories list
  4. Uncheck Display PDF in browser
  5. Press OK
  1. 打开 Adob​​e Reader
  2. 编辑菜单,首选项
  3. 从类别列表中选择 Internet
  4. 取消选中在浏览器中显示 PDF
  5. 按确定

you can also disable the Adobe plugin in Chrome, which will force Chrome to download the PDF.

您还可以在 Chrome 中禁用 Adob​​e 插件,这将强制 Chrome 下载 PDF。

It's under chrome://plugins.

它在 chrome://plugins 下。

but as you said its latest chrome browser then check is chrome PDF view visible in plugin if Yes disable it too.

但正如你所说的,它最新的 chrome 浏览器然后检查插件中是否可以看到 chrome PDF 视图,如果是的话也禁用它。

  • Disable "Chrome PDF Viewer" or unmark always allowed to run check box try both
  • 禁用“Chrome PDF 查看器”或取消标记始终允许运行复选框尝试两者

enter image description here

在此处输入图片说明

回答by Manu

You would need to add below statement to your chrome profile:

您需要将以下语句添加到您的 chrome 配置文件中:

chromePrefs.put("pdfjs.disabled", true);

It seems that newer versions of browsers are coming with built-in ability of displaying PDF files inside browser. Refer thisfor more information, though it is for firefox profile but still a good read.

似乎较新版本的浏览器具有在浏览器中显示 PDF 文件的内置功能。有关更多信息,请参阅信息,尽管它适用于 Firefox 配置文件,但仍然值得一读。

Hope that solves your issue, else you may need to downgrade your chrome to make it work. Let me know if you have any queries.

希望能解决您的问题,否则您可能需要降级 Chrome 以使其正常工作。如果您有任何疑问,请告诉我。

回答by rogercampos

Since chrome 57 the automatic pdf preview no longer works as a plugin, there's now a setting you can change to make this work. You can actually inspect the name of the pref by inspecting the chrome's own preference dialog, under "Content Settings" the flag that says "Open PDF files in the default PDF viewer application." automatic pdf open pref

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

You can set that to false to avoid automatic pdf preview, like this (ruby example):

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

回答by Arash Vahabpour

You might load this page and change the setting using selenium navigations:

您可以加载此页面并使用 selenium 导航更改设置:

chrome://settings/content/pdfDocuments

回答by windchaser

Here are the C# options for anyone working with .NET

以下是任何使用 .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);