Gradle+Selenium+ChromeDriver环境下HasVirtualAuthenticator缺失错误排查
问题
我是新手,想要获取指定URL的HTML内容,因此尝试使用Selenium编写了一个Spring Boot服务类ParserService。但运行时,在执行WebDriver webDriver = new ChromeDriver(options)代码时抛出错误:java.lang.NoClassDefFoundError: org/openqa/selenium/virtualauthenticator/HasVirtualAuthenticator。
服务代码
package dataox.keithgellmanrestaurantmenuscraping; import lombok.RequiredArgsConstructor; import lombok.log4j.Log4j2; import org.apache.commons.lang3.RandomUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import java.util.List; @Log4j2 @Service @RequiredArgsConstructor public class ParserService { private final ChromeOptions options = new ChromeOptions(); @EventListener(ApplicationReadyEvent.class) public void a() { String url = "some url"; WebDriver webDriver = getWebDriver(url); log.info(webDriver.getPageSource()); quitWebdriver(webDriver); } private WebDriver getWebDriver(String url) { WebDriver webDriver = new ChromeDriver(options); webDriver.get(url); return webDriver; } private void quitWebdriver(WebDriver webDriver) { webDriver.close(); webDriver.quit(); } }
错误信息
java.lang.reflect.UndeclaredThrowableException: Failed to invoke event listener method HandlerMethod details: Bean [dataox.keithgellmanrestaurantmenuscraping.ParserService] Method [public void dataox.keithgellmanrestaurantmenuscraping.ParserService.a()] Resolved arguments: Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/virtualauthenticator/HasVirtualAuthenticator
Gradle依赖配置
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' implementation group: 'org.seleniumhq.selenium', name: 'selenium-api', version: '3.141.59' implementation group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '3.141.59' implementation group: 'org.jsoup', name: 'jsoup', version: '1.15.3' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
解决方案
这个错误是因为Selenium依赖缺失,HasVirtualAuthenticator类属于Selenium的selenium-support模块,当前依赖中未引入该模块。
添加selenium-support依赖:在Gradle的dependencies块中加入以下配置,版本和其他Selenium依赖保持一致:
implementation group: 'org.seleniumhq.selenium', name: 'selenium-support', version: '3.141.59'统一依赖版本(可选):为避免版本冲突,确保所有Selenium相关依赖的版本完全相同。如果使用Spring Boot,也可以通过依赖管理统一控制版本号。
匹配ChromeDriver与Chrome浏览器版本:另外要注意,ChromeDriver版本必须和本地安装的Chrome浏览器版本对应,版本不匹配也会导致驱动启动失败。如果不想手动管理驱动版本,可以引入
webdrivermanager自动匹配:- 添加依赖:
implementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '5.3.2' - 修改代码,在创建ChromeDriver前调用setup方法:
private WebDriver getWebDriver(String url) { WebDriverManager.chromedriver().setup(); // 新增该行 WebDriver webDriver = new ChromeDriver(options); webDriver.get(url); return webDriver; }
- 添加依赖:
内容的提问来源于stack exchange,提问作者Liudmila Zavyalova
相关产品推荐
相关产品推荐

