使用Selenium Grid 4.8.1打开URL时间歇性出现HttpTimeoutException错误
解决Docker部署的Selenium Grid 4.8.1在RHEL上打开URL时的间歇性HttpTimeoutException
问题背景
- 运行环境:RHEL Linux + Docker + Selenium Grid 4.8.1
- 浏览器版本:Chrome 110
- 故障现象:打开URL环节间歇性抛出
java.net.http.HttpTimeoutException,重试get调用后仍出现相同错误
错误详情
java.net.http.HttpTimeoutException: request timed out Build info: version: '4.8.1', revision: '8ebccac989' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '<OS_VERSION>', java.version: '11.0.2' Driver info: org.openqa.selenium.remote.RemoteWebDriver Command: [0a902ee7ac88388d017f325cefd6e0d7, get {url=<APP_URL>}] Capabilities {acceptInsecureCerts: true, browserName: chrome, browserVersion: 110.0.5481.77, chrome: {chromedriverVersion: 110.0.5481.77 (65ed616c6e8e..., userDataDir: /tmp/.com.google.Chrome.D8sHzb}, goog:chromeOptions: {debuggerAddress: localhost:44241}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: LINUX, proxy: Proxy(), se:bidiEnabled: false, se:cdp: ws://<IP_ADDRESS>:4444/sessi..., se:cdpVersion: 110.0.5481.77, se:vnc: ws://<IP_ADDRESS>:4444/sessi..., se:vncEnabled: true, se:vncLocalAddress: ws://<IP_ADDRESS>:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true} Session ID: 0a902ee7ac88388d017f325cefd6e0d7
解决措施
1. 调整页面加载策略
当前pageLoadStrategy为normal(等待所有资源加载完成),若应用包含大量非关键资源(如广告、统计脚本),可改为eager(等待DOM加载完成)或none(不等待资源加载),降低超时概率:
ChromeOptions options = new ChromeOptions(); options.setPageLoadStrategy(PageLoadStrategy.EAGER); RemoteWebDriver driver = new RemoteWebDriver(new URL("http://grid-url:4444"), options);
2. 延长页面加载超时时间
当前配置的pageLoad超时为300000ms(5分钟),若应用本身加载较慢,可适当延长:
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.MINUTES);
3. 排查Docker网络连通性
- 确认Selenium Grid容器与目标应用网络互通,RHEL机器或Docker容器的防火墙/安全组未限制目标URL的访问
- 在容器内部执行curl命令验证网络稳定性:
docker exec -it <selenium-node-container-id> curl <APP_URL>
4. 升级组件版本
Selenium 4.8.1存在部分已知网络超时问题,升级至4.10+版本可修复兼容性问题;同时确保Chrome与ChromeDriver版本严格匹配,避免版本不一致引发异常。
5. 优化容器资源配置
为Selenium Node容器分配足够的CPU和内存,避免资源不足导致浏览器加载缓慢:
services: selenium-node-chrome: image: selenium/node-chrome:4.10.0-20230607 shm_size: '2g' # 增大共享内存,Chrome运行需要较大共享内存空间 mem_limit: 4g cpus: 2
6. 添加智能重试机制
在代码中针对get操作添加重试逻辑,区分临时超时与真实加载失败:
int retryCount = 3; boolean loaded = false; while (retryCount > 0 && !loaded) { try { driver.get("<APP_URL>"); loaded = true; } catch (HttpTimeoutException e) { retryCount--; if (retryCount == 0) throw e; Thread.sleep(2000); // 间隔2秒后重试 } }
内容的提问来源于stack exchange,提问作者Siva Sai Phalguna Kaipa
相关产品推荐
相关产品推荐

