构造函数 WebDriverWait(chromeDriver, int) 未定义

The constructor WebDriverWait(chromeDriver, int) is undefined

即使在 eclipse 中导入 WebDriverWait 也无法识别 IDE。

有人知道可能的原因并解决这个问题吗?

您的项目中缺少 WebDriverWait。 1.Check 你已经使用了导入语句 org.openqa.selenium.support.ui 2. 手动将 jar 添加到项目中(或者)如果它是基于 Maven 的项目使用 mvn clean install ,然后重新打开 IDE- 你应该没问题。

您正在尝试使用

new WebDriverWait(driver, 10);

它将调用此构造函数

  /**
   * Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
   * the 'until' condition, and immediately propagate all others.  You can add more to the ignore
   * list by calling ignoring(exceptions to add).
   *
   * @param driver The WebDriver instance to pass to the expected conditions
   * @param timeoutInSeconds The timeout in seconds when an expectation is called
   * @see WebDriverWait#ignoring(java.lang.Class)
   * @deprecated Instead, use {@link WebDriverWait#WebDriverWait(WebDriver, Duration)}.
   */
  @Deprecated
  public WebDriverWait(WebDriver driver, long timeoutInSeconds) {
    this(driver, Duration.ofSeconds(timeoutInSeconds));
  }

如您所见,它已在较新版本的 Selenium 弃用 即 Selenium 4

解法:

你应该使用这个构造函数:

  public WebDriverWait(WebDriver driver, Duration timeout) {
    this(
        driver,
        timeout,
        Duration.ofMillis(DEFAULT_SLEEP_TIMEOUT),
        Clock.systemDefaultZone(),
        Sleeper.SYSTEM_SLEEPER);
  }

您的有效代码:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));

应该会为您完成工作。

WebDriverWait w=new WebDriverWait(driver,10) 

基本上,这已被弃用。

可以通过以下方式更改上述语句来抑制此问题

WebDriverWait webdwait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebDriverWait webdwait = new WebDriverWait(driver, Duration.ofHours(10));