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
相关产品推荐
相关产品推荐

