【问题标题】:The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver> error for FluentWait Class through Selenium and JavaFluentWait 类型不是通用的;它不能通过 Selenium 和 Java 使用 FluentWait 类的参数 <WebDriver> 错误进行参数化
【发布时间】:2017-07-05 15:57:44
【问题描述】:

我正在与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 as The type FluentWait is not generic; it cannot be parameterized with arguments &lt;WebDriver&gt; 上遇到错误

这是我的进口清单:

    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 的修改构造函数添加了 answer

【问题讨论】:

  • @JeffC 谢谢。我可以看到 selenium-java-2.53.1 src 代码提供的文档与您共享的 URL 中提供的文档之间存在一些差异。在 selenium-java-2.53.1 src 代码中,创建对象是 wait = new FluentWait(driver) -->> 但链接文档说它是 >

标签: java selenium selenium-webdriver webdriver fluentwait


【解决方案1】:

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

代码:

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"));
            }
        }
    }
}

【讨论】:

  • 我已经编辑了代码或检查下面的代码是否有帮助? FluentWait wait = new FluentWait(driver) .withTimeout(100, TimeUnit.SECONDS) .pollingEvery(50, TimeUnit.MILLISECONDS); // 开始等待给定元素 wait.until(new ElementWaitCondition(browser, query));
  • 谢谢,如果我提供, wait = new FluentWait(driver)>> Eclipse IDE 仍然向我显示错误 The type FluentWait is not generic; it cannot be parameterized with arguments &lt;WebDriver&gt;
  • 我能够实现没有任何错误,我使用的是 Selenium-java 3.0.1 和服务器 3.0.1
  • 谢谢先生。为了更新您,到目前为止,如果我尝试导入 import org.openqa.selenium.support.ui.FluentWait;,我已经添加了以下导入 import org.openqa.selenium.support.ui.Wait; Eclipse IDE 显示错误为“导入 org.openqa.selenium.support.ui.FluentWait 与定义的类型冲突在同一个文件中”。请问还有什么建议吗?
  • 我做了两处更改,首先我删除了我怀疑是指接口的类文件“FluentWait.class”。创建了一个新的类文件“FluentWaitExample”,我添加了导入 import org.openqa.selenium.support.ui.FluentWait;import org.openqa.selenium.support.ui.Wait; 仍然在 (driver) >> 错误提示 FluentWait cannot be resolved to a type
【解决方案2】:

随着 Selenium v​​3.11.0 的推出,FluentWait 的构造函数发生了变化。现在withTimeoutpollingEvery 的参数类型是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"));
            }
        });

    }

}

【讨论】:

  • 感谢 DebanjanB,它消除了消息:“Warning:() java: withTimeout(long,java.util.concurrent.TimeUnit) in org.openqa.selenium.support.ui.FluentWait has been deprecated”
【解决方案3】:

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

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

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

【讨论】:

    【解决方案4】:

    我也遇到了同样的错误,后来我注意到我使用的类名是fluentwait。更改类名后,它工作正常。

    【讨论】:

    • 你把类名改成什么了,抱歉我是这个领域的新手?
    • @topsoftwarepro 将其更改为“FluentWait”以外的任何其他名称,例如您可以使用 Fluent_Wait
    【解决方案5】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 2019-01-17
      • 2015-06-15
      • 2013-04-30
      • 2019-06-18
      相关资源
      最近更新 更多