Selenium WebDriver结合Docker与Cucumber.io报错求助
解决
Selenium::WebDriver::Remote::Driver needs :options to be set (ArgumentError) 错误 这个错误源于Selenium 4+ 已废弃 :desired_capabilities 参数,且代码中未正确初始化 chrome_options 变量。以下是具体修复方案:
核心问题分析
- 你使用的
:desired_capabilities是 Selenium 3 及更早版本的语法,Selenium 4 强制要求通过:options传递浏览器配置 - 代码中直接引用未定义的
chrome_options变量,导致参数无效触发报错
修复后的 .env.rb 代码
require 'rspec' require 'capybara/cucumber' require 'selenium-webdriver' require 'pry' # 清除代理(无需代理时启用) # ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil # 获取 Docker 宿主机 IP docker_ip = %x(/sbin/ip route|awk '/default/ { print $3 }').strip # 初始化 Chrome 浏览器选项 chrome_options = Selenium::WebDriver::Chrome::Options.new # 可选:添加容器环境适配参数(无头模式、禁用沙箱等) chrome_options.add_argument('--headless=new') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') Capybara.register_driver :remote_chrome do |app| Capybara::Selenium::Driver.new( app, browser: :remote, url: "http://#{docker_ip}:4444/wd/hub", options: chrome_options ) end Capybara.configure do |config| config.run_server = false config.default_driver = :remote_chrome config.app_host = 'http://www.google.com' # 替换为你的目标应用地址 end
修复说明
- 新增
chrome_options初始化代码,采用 Selenium 4 官方推荐的Selenium::WebDriver::Chrome::Options类 - 移除废弃的
:desired_capabilities参数,仅保留:browser、:url、:options三个必要参数 - 添加容器环境适配的 Chrome 启动参数,避免因权限、资源限制触发额外错误
额外注意事项
- 确保
selenium-webdriver版本为 4.x,版本过低可执行bundle update selenium-webdriver升级 - 确认运行在 4444 端口的远程 Chrome 节点已正常启动,且支持你设置的浏览器选项
内容的提问来源于stack exchange,提问作者XmalevolentX
相关产品推荐
相关产品推荐

