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

Rails 7.2 RSpec无头Chrome测试报MultipleExceptionError求助

Rails 7.2无头Chrome Selenium RSpec系统测试突发"no such window"及"handle must be a string"错误

无代码及配置变更的情况下,Rails 7.2项目中基于无头Chrome Selenium Webdriver的RSpec系统测试突然抛出以下错误:

RSpec::Core::MultipleExceptionError: no such window
  (Session info: chrome=127.0.6533.88)

invalid argument: 'handle' must be a string
  (Session info: chrome=127.0.6533.88)

当前使用的Chrome无头驱动配置如下:

Capybara.register_driver :chrome_headless do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new

  options.add_argument("--headless=new") unless ENV["GUI"] == "on"
  options.add_argument("--no-sandbox")
  options.add_argument("--disable-dev-shm-usage")
  options.add_argument("--window-size=1400,1400")


  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

排查与解决方案

  • 匹配ChromeDriver与Chrome版本:Chrome 127.0.6533.88必须使用完全同版本的ChromeDriver,版本号不一致会导致会话异常。下载对应版本替换现有驱动即可。
  • 升级依赖包:运行bundle update selenium-webdriver capybara,确保依赖库适配最新Chrome版本。
  • 调整无头模式参数:临时将--headless=new替换为--headless=old测试是否解决问题,或添加额外参数:
    options.add_argument("--disable-gpu")
    options.add_argument("--remote-debugging-port=9222")
    
  • 检查窗口切换逻辑:若测试中存在switch_to.window操作,确认传入的窗口句柄是有效字符串,避免nil或非字符串值触发"handle must be a string"错误。
  • 清理测试会话:在测试套件启动前添加会话重置步骤,清除残留会话:
    Capybara.reset_sessions!
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 01:53:13