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

在Heroku的RoR项目中使用Selenium时出现connect_until_stable错误求助

问题:Heroku上运行Ruby+Selenium报错无法连接ChromeDriver

报错信息

/app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/service_manager.rb:130:in `connect_until_stable': unable to connect to /app/.cache/selenium/chromedriver/linux64/109.0.5414.74/chromedriver 127.0.0.1:9515 (Selenium::WebDriver::Error::WebDriverError)
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/service_manager.rb:57:in `block in start'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/socket_lock.rb:41:in `locked'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/service_manager.rb:54:in `start'
        from <internal:kernel>:90:in `tap'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/service.rb:83:in `launch'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/driver.rb:341:in `service_url'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/driver.rb:321:in `create_bridge'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/driver.rb:74:in `initialize'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/driver.rb:47:in `new'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver/common/driver.rb:47:in `for'
        from /app/vendor/bundle/ruby/3.1.0/gems/selenium-webdriver-4.7.1/lib/selenium/webdriver.rb:88:in `for'
        from bin/flights_data.rb:22:in `scraper'
        from bin/flights_data.rb:36:in `<main>'

已做配置

  • 已安装chromedriver buildpack和Chrome buildpack
  • Gemfile依赖配置:
gem 'selenium-webdriver'
  • 测试Ruby文件代码:
selenium

def selenium
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument('--headless')
    driver = Selenium::WebDriver.for :chrome, options: options
    puts "HELLO"
    open_google(driver)
end

def open_google(driver)
    driver.get 'https://www.google.com/flights'
end

错误原因分析

这个错误本质是Selenium无法与ChromeDriver建立稳定的网络连接,常见触发原因包括:

  • Chrome与ChromeDriver版本不匹配:Selenium要求浏览器和驱动版本严格对应,你当前使用的ChromeDriver是109.0.5414.74,但Heroku上安装的Chrome版本可能和它不兼容,导致驱动无法正常启动服务。
  • 缺少容器环境必需的启动参数:Heroku是无图形的容器环境,仅添加--headless不够,还需要--no-sandbox(禁用沙箱模式,容器内默认权限不足)和--disable-dev-shm-usage(解决容器内共享内存不足的问题),缺少这些参数会导致Chrome启动失败,进而ChromeDriver无法响应连接请求。
  • ChromeDriver路径或权限问题:虽然安装了buildpack,但可能Selenium没有正确识别到ChromeDriver的安装路径,或者驱动文件没有执行权限,导致无法启动服务。
  • Heroku资源限制:免费或基础型dyno内存有限,Chrome启动时需要较多资源,内存不足会导致ChromeDriver启动失败,无法建立连接。

修正建议示例

修改测试文件的Chrome参数,补充必要的容器适配参数:

def selenium
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--disable-gpu') # 部分环境需要,可选添加
    driver = Selenium::WebDriver.for :chrome, options: options
    puts "HELLO"
    open_google(driver)
end

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 11:05:24