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

Selenium升级到4.0.0报com.thoughtworks.selenium.webdriven包不存在如何解决

报错原因

com.thoughtworks.selenium.webdriven是Selenium 1.x(RC版本)的遗留兼容包,Selenium 2.x版本保留了该兼容层默认集成,从Selenium 3.0开始该包就从核心依赖中移除,4.0版本更是彻底停止了默认支持,因此升级后直接编译就会报包不存在的错误。

解决方案

1. 长期方案:迁移到Selenium 4标准WebDriver API(推荐)

  • 移除所有导入com.thoughtworks.selenium.*包的代码
  • 原有基于DefaultSelenium类的实例化逻辑全部替换为标准WebDriver实现,示例如下:
    旧RC版本代码:
    Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "https://example.com");
    selenium.start();
    
    替换为4.0标准代码:
    WebDriver driver = new FirefoxDriver();
    driver.get("https://example.com");
    
  • 常用操作方法对应替换规则:
    • selenium.click("定位符") → driver.findElement(By.定位方式("定位符")).click()
    • selenium.type("定位符", "输入值") → driver.findElement(By.定位方式("定位符")).sendKeys("输入值")
    • selenium.getText("定位符") → driver.findElement(By.定位方式("定位符")).getText()
    • selenium.waitForPageToLoad("超时时间") → 替换为WebDriverWait显式等待实现

2. 临时方案:引入遗留RC兼容包(仅过渡使用)

如果暂时没有时间完成全量代码重构,可以手动引入独立维护的遗留兼容依赖,Maven配置示例如下:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-leg-rc</artifactId>
    <version>4.0.0</version>
</dependency>

注意:该兼容包后续版本可能会被正式废弃,仅可作为短期过渡方案,不建议长期使用。

3. 验证修改

调整完成后清空项目原有编译缓存,重新执行编译即可:

  • Maven项目执行命令:mvn clean compile
  • Gradle项目执行命令:gradle clean build

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 01:18:01