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

标签 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

错误:

enter image description here enter image description here

最佳答案

对于 Selenium 4 & Chrome <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 switches list

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

关于ruby - 如何在 Ruby Selenium 中使用 Chrome 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45275586/

相关文章:

python - 在 Selenium 和 Python 中找到下一个兄弟元素?

ios - 如何以编程方式在 Safari 中启用 "Allow Remote Automation"

selenium - 选择文本并执行单击操作

java - 如何在java中使用selenium webdriver查找特定的表元素?

ruby-on-rails - Haml 更新后 Capybara::Poltergeist::MouseEventFailed (4.0.7 => 5.0.2) [RAILS]

ruby-on-rails - PGconn.connect ....断开连接在哪里?

python - 有没有可能为动态语言创建编译器而不失去他的动态特性?

ruby-on-rails - Rails 按列分组并选择列

java - 等待使用 HtmlUnitDriver 加载页面

java - 与implicitwait()同步不起作用,为什么?