FluentWait类型不是通用的;它不能通过Selenium和Java为FluentWait类的参数<webdriver>错误进行参数化

时间:2017-02-16 13:42:52

标签: java selenium selenium-webdriver webdriver fluentwait

我正在使用PortalId。我试图在我的代码中添加Selenium Standalone Server 3.0.1,以便在元素变得可见时通过xpath检测元素。为了获得一些Java帮助,我查找了Explicit Wait的源代码,但无法找到它。我在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

错误

以下是我的导入列表:

The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver>

有人可以帮帮我吗?

更新

Selenium v​​3.11.0

中添加了关于 FluentWait 的修改构造函数的answer

4 个答案:

答案 0 :(得分:3)

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

    }

}

答案 1 :(得分:2)

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

答案 2 :(得分: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"));
            }
        }
    }
}

答案 3 :(得分:0)

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

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

表格withTimeout(Duration timeOut) 仍在使用且不推荐使用