You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL2下RSpec+Capybara配置Selenium远程Chrome无头模式问题

解决WSL2中远程Selenium服务器的Headless配置问题

正确的Capybara配置方式

你遇到的未知关键字错误,是因为直接传递args: 'headless'的格式不符合远程Selenium驱动的配置要求。远程驱动需要将浏览器参数封装到对应的options对象中,而非作为顶级配置项传入。

Chrome浏览器配置示例

在你的spec/support/capybara.rb(或RSpec核心配置文件)中添加以下代码:

Capybara.register_driver :remote_chrome do |app|
  # 创建Chrome浏览器配置对象
  options = Selenium::WebDriver::Chrome::Options.new
  # 添加headless参数(新版Chrome用--headless=new,旧版用--headless)
  options.add_argument('--headless=new')
  # 可选:WSL环境推荐添加,避免权限/渲染问题
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-gpu')

  # 初始化远程Selenium驱动
  Capybara::Selenium::Driver.new(
    app,
    browser: :remote,
    url: 'http://你的远程Selenium服务器地址:4444/wd/hub', # 替换为实际服务器地址
    capabilities: options
  )
end

# 设置默认驱动和JS驱动为远程Chrome
Capybara.default_driver = :remote_chrome
Capybara.javascript_driver = :remote_chrome

关键注意点

  • 所有浏览器启动参数必须通过Selenium::WebDriver::Chrome::Options的add_argument方法添加,再通过capabilities参数传递给远程驱动,不能直接写args: 'headless'。
  • 若远程服务器上的Chrome版本低于112,需将--headless=new改为--headless。
  • 确保远程Selenium服务器已正常启动,且服务器上的ChromeDriver版本与Chrome浏览器版本完全匹配。

内容的提问来源于stack exchange,提问作者user12685852

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 10:56:06