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

Aha*_*hat 1 java eclipse selenium selenium-webdriver

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

在此输入图像描述

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

cru*_*dey 8

您正在尝试使用

new WebDriverWait(driver, 10);
Run Code Online (Sandbox Code Playgroud)

这将调用这个构造函数

  /**
   * 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));
  }
Run Code Online (Sandbox Code Playgroud)

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

解决方案:

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

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

您的有效代码:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
Run Code Online (Sandbox Code Playgroud)

应该为你完成工作。