运行 RemoteDriver 时 Chrome 选项不提示下载

ChromeOptions To Not Prompt For Download When Running RemoteDriver

我正在尝试调试为什么当 运行 远程 webdriver 在无头 linux 主机上进行测试时,chrome 中显示了下载对话框。我相信 chrome 版本是 45.

几个环境细节

  1. 硒 2.53 (gem)
  2. Selenium 2.53 服务器 Jar
  3. Chrome 驱动程序 2.21

framework/Tests 是用 Ruby 编写的,利用 Capybara 驱动网络测试。这是远程驱动程序如何初始化的简短片段。

            prefernces = {
          :download => {
            :prompt_for_download => false, 
            :default_directory => '/home/john.doe/Downloads/'
          }
        }
        caps = Selenium::WebDriver::Remote::Capabilities.chrome()
        caps['chromeOptions'] = {'prefs' => prefernces}

      http_client = Selenium::WebDriver::Remote::Http::Default.new
      http_client.timeout = 240
      options = {
        browser: :remote,
        url: "http://<server_url>:4444/wd/hub",
        desired_capabilities: caps,
        http_client: http_client
      }
      # Returns Remote Driver
      Capybara::Selenium::Driver.new(app, options)

我已通过集线器验证 chrome选项已设置,但在下载文件时,我们会看到一个文件对话框提示。

我一直在寻找解决这个问题的方法。感谢您的帮助和考虑!

尝试删除 default_directory 末尾的 / 并设置 directory_upgrade: true。除此之外,确保浏览器有权写入所选目录(还要注意首选项的正确拼写)

 preferences = {
      :download => {
        :default_directory => '/home/john.doe/Downloads',
        :directory_upgrade => true,
        :prompt_for_download => false, 

      }
    }
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
  'chromeOptions' => {'prefs' => preferences}
)

这是一个使用 Capybara / Selenium / Chrome 下载文件的示例:

require 'capybara'
require 'selenium-webdriver'

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app,
    :url => "http://localhost:4444/wd/hub",
    :browser => :chrome,
    :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
      'chromeOptions' => {
        'prefs' => {
          'download.default_directory' => File.expand_path("C:\Download"),
          'download.directory_upgrade' => true,
          'download.prompt_for_download' => false,
          'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
        }
      }
    )
  )
end

session = Capybara::Session.new(:chrome)
session.visit "https://www.mozilla.org/en-US/foundation/documents"
session.click_link "IRS Form 872-C"

sleep 20