【问题标题】:Selenium WebDriver (Ruby): How to download PDF files?Selenium WebDriver (Ruby):如何下载 PDF 文件?
【发布时间】:2017-03-03 00:30:33
【问题描述】:

当使用 Chrome 运行 WebDriver (Ruby) 时,有哪些特定的命令行开关desired_capabilities 可以禁用 Chrome 的内置 PDF 查看器。

换句话说,如何以编程方式将 Chrome 配置为下载 PDF 文件,而不是使用其内部的 PDF-viewer 插件打开?

【问题讨论】:

标签: google-chrome selenium-webdriver plugins capability


【解决方案1】:

更新答案:Chrome 61:Ruby 2.3:ChromeDriver 2.32.498537

      download_prefs = {
        prompt_for_download: false,
        default_directory: 'desired/download/path'
      }

      plugin_prefs = {
        always_open_pdf_externally: true
      }

      options = Selenium::WebDriver::Chrome::Options.new
      options.add_preference(:download, download_prefs)
      options.add_preference(:plugins, plugin_prefs) 
      driver = Selenium::WebDriver.for :chrome, options: options

【讨论】:

  • 为什么我看到uninitialized constant Selenium::WebDriver::Chrome::Options,即使我有require 'selenium-webdriver'
【解决方案2】:

根据Save PDF instead of opening in Selenium 提供的以下资源,我发现这可以正常工作

prefs = {
    plugins: {
      plugins_disabled: ['Chrome PDF Viewer']
    },
    download: {
      prompt_for_download: false,
      directory_upgrade:   true,
      default_directory:   'desired/download/path'
    }
}

browser = Selenium::WebDriver.for :chrome, prefs: prefs

目录路径中必须有斜杠

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2018-03-08
    • 2014-06-24
    • 2018-04-14
    • 2019-12-28
    相关资源
    最近更新 更多