【问题标题】:How to use Chrome Options in Ruby Selenium?如何在 Ruby Selenium 中使用 Chrome 选项?
【发布时间】:2017-12-29 17:51:36
【问题描述】:

以下sn-p来自官方页面Ruby Bindings;但是,它无法正常工作

  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument('--ignore-certificate-errors')
  options.add_argument('--disable-popup-blocking')
  options.add_argument('--disable-translate')
  @driver = Selenium::WebDriver.for :chrome, options: options

错误:

【问题讨论】:

  • 您是否禁用了自动加载?所有不同的浏览器(包括它们的选项)都会自动加载。例如 Chrome:autoload :Chrome, 'selenium/webdriver/chrome'
  • 不确定;但为什么要自动加载?如果禁用自动加载,选项不起作用?
  • 是的,自动加载应该记录Selenium::WebDriver::Chrome::Options 存在的事实,但只会在您使用它时加载它。如果自动加载以某种方式被破坏,那么这将不起作用
  • 那么,如何开启呢?
  • 啊!你的意思是在红宝石中自动完成?我不认为这是一个问题,因为我在通过终端执行时出错。你可以找到有问题的输出

标签: ruby selenium selenium-webdriver


【解决方案1】:

对于Selenium 4Chrome <75 用户

  options = {
      args: ['disable-infobars', 'disable-gpu', 'privileged', 'ignore-certificate-errors', 'no-default-browser-check'],
      w3c: true,
      mobileEmulation: {},
      prefs: {
          :protocol_handler => {
              :excluded_schemes => {
                  tel: false,
              }
          }
      },
      extensions: [ Base64.strict_encode64(File.open("../your_extension.crx", 'rb').read) ]
  }

  caps = Selenium::WebDriver::Chrome::Options.new(options: options)
  @driver = Selenium::WebDriver.for(:chrome, options: caps)

对于Selenium 3 用户

使用开关来定义 chrome 选项

  caps = Selenium::WebDriver::Remote::Capabilities.chrome("desiredCapabilities" => {"takesScreenshot" => true}, "chromeOptions" => {"binary" => "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"})
  @driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps, switches: %w[--incognito --screen-size=1200x800]

或者

driver = Selenium::WebDriver.for :chrome, switches: %w[--incognito]

RemoteWebDriver

caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "disable-infobars" ]})
driver = Selenium::WebDriver.for :remote, url: 'http://localhost:4444/wd/hub', desired_capabilities: caps

Chrome 开关列表

https://peter.sh/experiments/chromium-command-line-switches/

【讨论】:

  • 仅供参考,在 selenium 的最新版本中:WARN Selenium [DEPRECATION] :args or :switches is deprecated. Use Selenium::WebDriver::Chrome::Options#add_argument instead. 另一种方法是 Selenium::WebDriver::Chrome::Options.new(args: [<args>])
【解决方案2】:

这对我有用:

args = ['ignore-certificate-errors', 'disable-popup-blocking', 'disable-translate']

options = Selenium::WebDriver::Chrome::Options.new(args: args)
driver = Selenium::WebDriver.for :chrome, options: options

【讨论】:

    猜你喜欢
    • 2011-06-08
    • 2014-10-09
    • 2016-04-22
    • 1970-01-01
    • 2017-01-15
    • 2011-04-05
    • 2017-05-21
    • 1970-01-01
    • 2013-03-27
    相关资源
    最近更新 更多