Selenium WebDriver运行失败:dev/failsafe/ClassNotFoundException问题求助
Selenium启动报错:java.lang.NoClassDefFoundError: dev/failsafe/Policy
问题原因
这个报错是因为你用的Selenium 4.x版本依赖Failsafe类库,但你的项目只引入了Selenium核心JAR,没添加配套的依赖JAR包。Selenium从4.x开始不再是单一JAR就能运行,必须依赖多个第三方库。
解决步骤(针对Eclipse+Java8环境)
- 找到你下载的
selenium-java-4.15.0压缩包,解压后里面有个libs文件夹,里面包含所有Selenium运行必需的依赖JAR,其中就有failsafe-*.jar - 打开Eclipse,右键你的项目 → 选择「Build Path」→「Add External Archives」
- 进入
selenium-java-4.15.0/libs路径,选中所有JAR文件,点击确定添加到项目中
额外优化建议
- 自动驱动管理(Selenium 4.6+支持):你代码里手动设置
webdriver.chrome.driver的那行可以删掉,Selenium 4.6及以上版本会自动识别并下载匹配的ChromeDriver,不用再手动配置路径。 - 依赖管理工具(推荐):后续可以改用Maven或Gradle构建项目,只需要添加Selenium的依赖配置,就能自动下载所有需要的JAR,不用手动找依赖。比如Maven的pom.xml配置:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.15.0</version> </dependency>
- 版本匹配检查:确保ChromeDriver版本和你电脑上的Chrome浏览器主版本一致(比如Chrome 119对应ChromeDriver 119.x),避免版本不兼容问题。
修正后可运行的代码
package newAttemptPackage; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class NewTryClass { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); driver.get("https://www.google.com/"); driver.manage().window().maximize(); driver.close(); } }
内容的提问来源于stack exchange,提问作者God Gamer
相关产品推荐
相关产品推荐

