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

Selenium报错:WebDriverWait(WebDriver, int)构造方法未定义如何解决?

WebDriverWait构造方法报错解决方案

问题说明

WebDriverWait(WebDriver, int)这个构造方法在Selenium 4.x版本已被废弃并移除,这就是你触发"The constructor WebDriverWait(WebDriver, int) is undefined"错误的原因。

修改后的代码

import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

// 初始化等待对象
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("above-expath")));
element.click();

关键修改点

  • 把原构造方法中int类型的等待时长替换为Duration对象,比如Duration.ofSeconds(10)表示等待10秒,还可通过ofMillis()、ofMinutes()等方法指定不同时间单位的时长
  • 需导入java.time.Duration类(Java 8及以上版本自带)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 10:30:29