Selenium从3.141.59升级到4.1.0后出现ClassNotFoundException求助
Selenium 4.1.0升级后出现NoClassDefFoundError:缺失AsyncHttpClient依赖
错误详情
minated> TestRunner [TestNG] C:\Program FilesVava\jdk1.8.0_321\bin\javaw.exe (Nov 18, 2022, 6:46:33 PM) java.lang.NoClassDefFoundError: org/asynchttpclient/DefaultAsyncHttpClientConfig$Builder at org.openqa.selenium.remote.http.netty.NettyClient.createHttpClient(NettyClient.java:91) at org.openqa.selenium.remote.http.netty.NettyClient.<clinit>(NettyClient.java:57) at org.openqa.selenium.remote.http.netty.NettyClient$Factory.createClient(NettyClient.java) at org.openqa.selenium.remote.tracing.TracedHttpClient$Factory.createClient(TracedHttpClient) at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:107) at org.openqa.selenium.remote.RemoteWebDriver.createTracedExecutorWithTracedHttpClient(RemoteWebDriver.java) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:146) at com.cucumber.utility.BrowserFactory.launchBrowser(BrowserFactory.java:82) at com.cucumber.step.StepDefinition_.launch_the_browser_and_navigate_to_ at ?.Launch the browser "yes" "Chrome" "105.0" "Windows 10" and navigate to URL Env "h" Caused by: java.lang.ClassNotFoundException: org.asynchttpclient.DefaultAsyncHttpClientConfig$Builder at java.net.URLClassLoader.findClass(URLClassLoader.java:387) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ... 15 more
问题原因
Selenium 4.x开始默认使用Netty HttpClient作为远程通信的HTTP客户端实现,该实现依赖org.asynchttpclient库。升级后未正确引入该依赖,或存在依赖版本冲突,导致JVM找不到对应的类。注释okhttp3代码无法解决问题,因为Selenium 4.1.0已不再依赖okhttp3,核心问题是缺失AsyncHttpClient。
解决方案
1. 正确引入AsyncHttpClient依赖
根据你的依赖管理工具(Maven/Gradle),添加适配Selenium 4.1.0的AsyncHttpClient依赖:
Maven 配置
推荐使用Selenium BOM统一管理版本,避免版本不匹配:
<dependencyManagement> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.1.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> </dependency> </dependencies>
也可以手动指定依赖版本:
<dependency> <groupId>org.asynchttpclient</groupId> <artifactId>async-http-client</artifactId> <version>2.12.3</version> </dependency>
Gradle 配置
// 使用Selenium BOM统一管理版本 implementation platform('org.seleniumhq.selenium:selenium-java:4.1.0') implementation 'org.seleniumhq.selenium:selenium-java'
2. 排查并解决依赖冲突
如果项目中其他依赖引入了不同版本的AsyncHttpClient,会导致冲突。可以通过以下命令排查:
- Maven:执行
mvn dependency:tree | grep asynchttpclient定位冲突来源 - Gradle:执行
./gradlew dependencies | grep asynchttpclient定位冲突来源
找到冲突依赖后,在对应的依赖中排除AsyncHttpClient:
<!-- Maven示例:排除冲突的AsyncHttpClient依赖 --> <dependency> <groupId>冲突依赖的groupId</groupId> <artifactId>冲突依赖的artifactId</artifactId> <version>冲突依赖版本</version> <exclusions> <exclusion> <groupId>org.asynchttpclient</groupId> <artifactId>async-http-client</artifactId> </exclusion> </exclusions> </dependency>
3. 清理本地依赖缓存
如果本地仓库存在损坏的依赖文件,清理后重新下载:
- Maven:删除本地仓库中
org/asynchttpclient和org/seleniumhq/selenium目录,重新执行mvn clean install - Gradle:执行
./gradlew clean build --refresh-dependencies
内容的提问来源于stack exchange,提问作者shri
相关产品推荐
相关产品推荐

