Selenium WebDriver(Ruby):如何下载PDF文件?

时间:2017-03-03 00:30:34

标签: google-chrome selenium-webdriver plugins capability

使用Chrome运行WebDriver( Ruby )时,具体的命令行开关desired_capabilities可禁用Chrome的内置功能PDF查看器。

换句话说,如何以编程方式配置Chrome以下载PDF文件而不是使用其内部PDF查看器插件进行打开?

2 个答案:

答案 0 :(得分:2)

更新的答案: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

答案 1 :(得分:1)

根据提供的以下资源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

目录路径中必须有尾部斜杠