如何在Ruby Selenium中使用Chrome选项?

Pra*_*ams 3 ruby selenium selenium-webdriver

下面的摘录来自官方页面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
Run Code Online (Sandbox Code Playgroud)

错误:

在此处输入图片说明 在此处输入图片说明

Pra*_*ams 6

对于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)
Run Code Online (Sandbox Code Playgroud)

对于Selenium 3用户

使用开关定义镶边选项

  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]
Run Code Online (Sandbox Code Playgroud)

要么

driver = Selenium::WebDriver.for :chrome, switches: %w[--incognito]
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Chrome开关清单

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

  • 仅供参考,在最新的硒版本中:不建议使用WARN Selenium [DEPRECATION]:args或:switches。改用Selenium :: WebDriver :: Chrome :: Options#add_argument。另一种方法是`Selenium :: WebDriver :: Chrome :: Options.new(args:[&lt;args&gt;])。 (2认同)