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

Selenium 4.3.0中WebDriverWait构造方法报NoSuchMethodError求助

WebDriverWait构造方法调用报错解决

环境信息

  • Eclipse版本:2021-12(4.22.0)
  • ChromeDriver版本:103.0.5060.134
  • Chrome浏览器版本:103.0.5060.134
  • Selenium版本:4.3.0

代码片段

import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

public void enterUsr(String user)  {

    WebDriverWait myWait = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(10));
    WebElement inputUsr =  myWait.until(ExpectedConditions.elementToBeClickable(By.name("username")));
    inputUsr.sendKeys(user);
}

报错信息

java.lang.NoSuchMethodError: 'void org.openqa.selenium.support.ui.WebDriverWait.<init>(org.openqa.selenium.WebDriver, java.time.Duration)'
    at Paginas.PageLoginKop.achicarOpcionPolicies(PageLoginKop.java:88)
    at Paginas.PageLoginKop.login(PageLoginKop.java:98)

解决方法

  1. 排查依赖冲突
    该错误核心是JVM找不到指定的WebDriverWait构造方法,大概率是项目中混入了不同版本的Selenium依赖(比如旧版本的selenium-support包和4.3.0的核心包共存)。检查项目的依赖管理工具(Maven/Gradle)或Eclipse构建路径,确保所有Selenium相关依赖版本统一为4.3.0,移除重复或版本不一致的Jar包。

  2. 修正构造方法调用
    Selenium 4.x中WebDriverWait的构造方法有两种正确调用方式:

    • 仅设置超时时间:
      WebDriverWait myWait = new WebDriverWait(driver, Duration.ofSeconds(10));
      
    • 同时设置超时时间和轮询间隔(推荐用Builder模式,逻辑更清晰):
      WebDriverWait myWait = new WebDriverWait(driver, Duration.ofSeconds(10))
                                  .pollingEvery(Duration.ofSeconds(10));
      

    避免直接传入三个参数的旧写法,或确认参数顺序完全匹配对应构造方法。

  3. 清理依赖缓存
    清理Maven/Gradle的本地依赖缓存,重新下载所有依赖,确保依赖文件完整无损坏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 18:19:47