How to use Chrome Options in Ruby Selenium?
Asked Answered
U

3

9

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

Undermanned answered 24/7, 2017 at 8:23 Comment(6)
Do you have autoloading disabled? All of the different browsers (including their options) are autoloaded in. Chrome for example: autoload :Chrome, 'selenium/webdriver/chrome'Vaporization
am not sure; but why autoload? options doesnt work if autoload is disabled?Undermanned
Yes, autoload should register the fact that Selenium::WebDriver::Chrome::Options exists but will only load it when you use it. If autoload is broken somehow then this won't workVaporization
so, how to enable it?Undermanned
ah! you mean auto-complete in ruby? I dont think its an issue, cos i get error during execution through terminal. you can find the output in questionUndermanned
I'm not sure if autoload can be disabled but it might not be working properly for some reason. If no one else comes along to provide an answer, you could always gem unpack into your directory and edit the autoload line to be require insteadVaporization
U
12

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/

Undermanned answered 24/7, 2017 at 14:44 Comment(1)
FYI, in recent versions of selenium: WARN Selenium [DEPRECATION] :args or :switches is deprecated. Use Selenium::WebDriver::Chrome::Options#add_argument instead. Another way is Selenium::WebDriver::Chrome::Options.new(args: [<args>]).Clintonclintonia
S
3

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
Sasser answered 23/8, 2017 at 12:8 Comment(0)
S
0

This just worked for me. I can run Selenium on a Mac in Ruby without having to keep logging into the website I'm testing!

    dir = "~/Library/Application Support/Google/Chrome"
    options = Selenium::WebDriver::Chrome::Options.new(args: ["user-data-dir=#{dir}"])
    driver = Selenium::WebDriver.for(:chrome, capabilities: options)

Got this code from the

Selenium::WebDriver::Chrome::Options API Reference

Sissel answered 13/1, 2023 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.