为 Selenium/Ruby 脚本维护匹配 Chromedriver+Chrome 版本的最佳实践

Best Practice to Maintain Matching Chromedriver+Chrome Versions for Selenium/Ruby Script

我有一个非常简单的 Selenium/Ruby 脚本,具有以下关键行摘录:

#!/usr/bin/env ruby
...
require 'selenium-webdriver'
...
options = Selenium::WebDriver::Chrome::Options.new
...
driver = Selenium::WebDriver.for :chrome, options: options
...
driver.quit

很简单,它只是使用操作系统的 (Ubuntu Linux) 默认产品 Chrome 和 Chrome 驱动程序。

时不时地,Chrome 会自动更新,而 Chrome 驱动程序不会,导致脚本在 运行:

时启动失败
This version of ChromeDriver only supports Chrome version XX
Current browser version is YY with binary path /usr/bin/chromium-browser

每次手动更新Chrome驱动程序对我来说是微不足道的,但很烦人。

自动保持 Chrome 驱动程序更新以匹配 Chrome 的最佳实践方法是什么?

通常在 Python 中我们使用 auto installer 请参阅 here,这基本上是针对适当的 chrome 浏览器版本自动安装 chromdriver。我们在 Java-Selenium 绑定以及使用 WebdriverManager.

中具有相同的功能

由于您只在 ruby-Selenium bindings 中请求,我可以找到一个 Github 存储库,请参阅 here

运行 Selenium 通过自动安装和更新所有受支持的 webdriver 更容易测试。

  1. chromedriver
  2. 壁虎driver
  3. IEDriverServer
  4. msedgedriver

适用于 macOSLinuxWindowsWindows Subsystem for Linux (WSL) v1v2。并在底部查看浏览器和 OS 特定注释。

用法

在您的 Gemfile 中:

gem 'webdrivers', '~> 4.0', require: false

在您的项目中:

require 'webdrivers'

当您通过 Selenium 启动浏览器时,driver现在将自动下载或更新。