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

Selenium+WebDriverManager部署Linux(JBoss)超时问题求助

Selenium在Linux JBoss环境下连接超时问题排查与解决

问题描述

在Windows环境运行Selenium相关代码及依赖时一切正常,但将Java应用部署到Linux服务器的JBoss中后,出现连接超时错误。

依赖配置

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
</dependency>
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.2.1</version>
</dependency>

核心代码

WebDriverManager.chromedriver().setup();

ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
options.addArguments("--headless");
options.addArguments("--user-agent=MiUserAgentPersonalizado");
WebDriver driver = new ChromeDriver(options);

driver.get("http://www.xxx.xxx.xx/xxxx/HPSGDec10A");

报错信息

io.github.bonigarcia.wdm.versions.VersionDetector - Error reading commands.properties, using local instead
 WARN  io.github.bonigarcia.wdm.versions.VersionDetector - Exception reading https://chromedriver.storage.googleapis.com/LATEST_RELEASE to get latest version of chromedriver (Connect to https://chromedriver.storage.googleapis.com:443 [chromedriver.storage.googleapis.com/64.233.186.128, chromedriver.storage.googleapis.com/64.233.190.128, chromedriver.storage.googleapis.com/142.250.0.128, chromedriver.storage.googleapis.com/142.251.0.128, chromedriver.storage.googleapis.com/172.217.192.128] failed: connect timed out)

解决方案

1. 排查网络访问限制

Linux服务器大概率无法访问谷歌的chromedriver存储地址,先通过以下命令验证:

curl https://chromedriver.storage.googleapis.com/LATEST_RELEASE

如果无法访问,可通过两种方式解决:

  • 联系运维开放服务器到该域名的443端口网络权限;
  • 给WebDriverManager配置国内镜像源,替换默认的谷歌存储地址:
    WebDriverManager.chromedriver()
        .setMirrorUrl("https://npm.taobao.org/mirrors/chromedriver/")
        .setup();
    

2. 安装对应版本的Chrome浏览器

Linux服务器必须安装Chrome浏览器,WebDriverManager会根据本地Chrome版本匹配下载对应的chromedriver:

  • CentOS安装命令:
    yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
    
  • Ubuntu安装命令:
    apt-get update && apt-get install google-chrome-stable -y
    

安装后验证版本:

google-chrome --version

3. 显式指定chromedriver版本

避免自动检测时的网络请求,直接指定与本地Chrome匹配的chromedriver版本:

WebDriverManager.chromedriver()
    .driverVersion("114.0.5735.90") // 替换为实际Chrome版本对应的chromedriver版本
    .setup();

4. 补充无头模式必要参数

Linux环境下无头模式需要添加额外参数,避免权限或资源问题:

ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
options.addArguments("--headless");
options.addArguments("--user-agent=MiUserAgentPersonalizado");
// 新增参数
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-gpu"); // 部分Linux环境需要
WebDriver driver = new ChromeDriver(options);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 14:10:20