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

同一Docker网络中Rails应用无法访问Standalone Selenium的问题

问题排查与解决步骤

1. 修正Selenium端点路径(适配Selenium 4.x)

你的Rails代码中使用的http://selenium:4444/wd/hub是Selenium 3.x的旧端点,Selenium 4.x已废弃该路径,直接使用根路径即可:

driver = Selenium::WebDriver.for(:remote, url: 'http://selenium:4444', http_client: client, options:)

2. 解决会话槽不足导致的排队超时

从Selenium日志可见:

Unable to find a free slot for request d98fcf3f-81cd-4e64-9aa0-3d38d7fc784c.
max-sessions = 1

当前节点仅允许1个并发会话,且之前的会话可能未正常释放,导致新请求排队超时:

  • 先重启Selenium容器释放卡住的会话:
    docker-compose restart selenium
    
  • 若需要多会话,在docker-compose中添加环境变量调整上限:
    selenium:
      image: selenium/standalone-chrome
      # ...其他配置...
      environment:
        - SE_NODE_MAX_SESSIONS=2  # 根据服务器性能调整数值
    

3. 修复ChromeDriver与Chrome版本不兼容问题

日志明确警告版本不匹配:

The chromedriver version (115.0.5790.102) detected in PATH might not be compatible with the detected chrome version (115.0.5790.110)

版本不兼容会导致会话创建失败,触发500错误和超时:

  • 更换为版本同步的Selenium镜像,推荐使用最新版自动匹配:
    selenium:
      image: selenium/standalone-chrome:latest
      # ...其他配置...
    

4. 确认容器网络连通性

确保Rails容器与Selenium容器在同一Docker网络:

  • 若Rails使用了自定义网络,需将Selenium加入该网络:
    services:
      rails:
        # ...Rails配置...
        networks:
          - my_custom_network
      selenium:
        # ...Selenium配置...
        networks:
          - my_custom_network
    networks:
      my_custom_network:
        driver: bridge
    
  • 在Rails容器内测试连通性:
    docker-compose exec rails ping selenium
    docker-compose exec rails curl http://selenium:4444/status
    
    若curl返回正常JSON状态,说明网络无问题。

5. 调整HTTP客户端超时时间

完成以上步骤后仍超时的话,延长客户端超时阈值:

client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 300  # 设置为5分钟
client.open_timeout = 60
driver = Selenium::WebDriver.for(:remote, url: 'http://selenium:4444', http_client: client, options:)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 18:44:53