Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Chrome Options in Ruby Selenium?

The below snippet is from official page Ruby Bindings; however, it fails to work

  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

Error:

enter image description here enter image description here

like image 969
Prashanth Sams Avatar asked Jul 24 '17 08:07

Prashanth Sams


2 Answers

For Selenium 4 & Chrome <75 users

  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)

For Selenium 3 users

Used switches to define chrome options

  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]

Or

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 switches list

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

like image 174
Prashanth Sams Avatar answered Sep 18 '22 22:09

Prashanth Sams


This works for me:

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

options = Selenium::WebDriver::Chrome::Options.new(args: args)
driver = Selenium::WebDriver.for :chrome, options: options
like image 40
user3797120 Avatar answered Sep 20 '22 22:09

user3797120