ChromeOptions在运行RemoteDriver时未提示下载

时间:2016-04-27 22:15:06

标签: ruby selenium-webdriver capybara selenium-chromedriver chrome-options

我试图调试为什么在无头linux主机上运行远程webdriver测试时,Chrome会显示下载对话框。我相信Chrome版本是45。

Env详情

  1. Selenium 2.53(宝石)
  2. Selenium 2.53 Server Jar
  3. Chrome驱动程序2.21
  4. 框架/测试是使用Capybara用Ruby编写的,用于驱动Web测试。以下是远程驱动程序初始化方式的简要摘录。

                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)
    

    我已通过集线器验证了chromeOptions已设置,但是当下载文件时,我们会显示文件对话框提示。

    我已经烧蜡烛寻找解决这个问题的方法。感谢您的帮助和考虑!

2 个答案:

答案 0 :(得分:3)

尝试从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}
)

答案 1 :(得分:1)

以下是使用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