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

标签 java selenium selenium-webdriver webdriver fluentwait

我正在与 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 上收到错误类为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;

谁能帮帮我吗?

<小时/>

更新

添加了answer关于 Selenium v​​3.11.0

FluentWait 的修改构造函数

最佳答案

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

    }

}

关于java - FluentWait 类型不是通用的;无法通过 Selenium 和 Java 使用 FluentWait 类的参数 <WebDriver> 错误进行参数化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775767/

相关文章:

java - 用于 Eclipse 的谷歌插件 : how to add JVM arguments in the Run configuration

java - Selenium 网络驱动程序 : finding checkbox based on multiple row values

java - 如何选择由脚本 selenium webdriver 生成的元素

selenium - 无法在 Selenium 上获取 moveToElement 显示 SVG 元素的工具提示

java - 从套接字读取时线程挂起。它与 https ://bugs. openjdk.java.net/browse/JDK-8049846 有关吗?

java - 优先队列 Java 添加方法抛出语法错误和错位构造错误

perl - 如何禁止在 Selenium RC 窗口中显示密码

javascript - Selenium / python : JavaScript href refuses to execute

selenium - 如何在 selenium webdriver 中等待,直到输入 5 个字符长度的文本

Java Sax 解析器 - 在 EndElementListener 中停止处理