FluentWait 类型不是通用的; FluentWait Class through Selenium 和 Java 无法使用参数 <WebDriver> 错误进行参数化

The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver> error for FluentWait Class through Selenium and Java

我正在与 Selenium Standalone Server 3.0.1 合作。我试图在我的代码中添加一个 Explicit Wait 以在元素变得可见时通过 xpath 检测该元素。为了获得一些 Java 帮助,我查找了 Selenium Standalone Server 3.0.1 的源代码,但找不到。我在 selenium-java-2.53.1 版本中找到了源代码。我下载了它并找到 selenium-java-2.53.1-srcs 并添加到我的 Eclipse IDE。在 FluentWait 的帮助下,我只是将代码复制粘贴到 Eclipse IDE 中并更改了变量名。

文档中的示例代码如下:

   // Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
      }
    });

但是当我实现这段代码时,只需复制粘贴即可:

       Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
           .withTimeout(30, TimeUnit.SECONDS)
           .pollingEvery(5, TimeUnit.SECONDS)
           .ignoring(NoSuchElementException.class);

       WebElement element = wait.until(new Function<WebDriver, WebElement>()        {
         public WebElement apply(WebDriver driver) {
           return driver.findElement(By.xpath("//p[text()='WebDriver']"));
         }
       });

我在 FluentWait Class 上遇到错误,因为 The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver>

这是我的进口清单:

    import java.util.concurrent.TimeUnit;
    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.Wait;
    import com.google.common.base.Function;

谁能帮帮我?


更新

Selenium v​​3.11.0

中针对 FluentWait 的修改构造函数添加了一个

您需要在等待中指定预期条件,下面是可以解决您问题的修改代码。

代码:

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

public class DummyClass
{
    WebDriver driver;
    @Test
    public void test()
    {
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

        until(new Function<WebElement, Boolean>() 
        {
            public Boolean apply(WebElement element)
            {
                return element.getText().endsWith("04");
            }

            private void until(Function<WebElement, Boolean> function)
            {
                driver.findElement(By.linkText("Sample Post2"));
            }
        }
    }
}

有了 Selenium v​​3.11.0 FluentWait have changed. Now the argument type for withTimeout and pollingEvery is Duration 的构造函数。这是修改后的实现:

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import com.google.common.base.Function;

public class Fluent_Wait {

    public static void main(String[] args) {


        System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
            driver.get("https://www.google.com");
            // Waiting 30 seconds for an element to be present on the page, checking
            // for its presence once every 500 milliseconds.
            Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(Duration.ofSeconds(30))
            .pollingEvery(Duration.ofMillis(500))
            .ignoring(NoSuchElementException.class);

            WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
            public WebElement apply(WebDriver driver) {
                return driver.findElement(By.name("q"));
            }
        });

    }

}

最简单的解决方案是使用其他方法实现:

withTimeout(Duration.ofSeconds(10))
            .pollingEvery(Duration.ofSeconds(2))

表格withTimeout(Duration timeOut) 仍在使用且未弃用

我也遇到了同样的错误,后来我发现我使用的是 class 名称作为 fluentwait。更改 class 名称后,它工作正常。

我也遇到了与 The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver> 相同的错误,但我注意到一个愚蠢的错误,即我的主要 class 也被命名为 FuentWait 并且它不是通用的。我更改了它的名称,错误消失了。