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 - Android内存泄漏将匿名类实现与特定于线程的局部变量一起使用

java - 从某个点记忆一个方法

java - - java.lang.NullPointerException - 空对象引用上的 setText

java - 清除表并重置自增主键

java - 使用 Java 在 Selenium WebDriver 中进行图像比较

selenium - 错误 : self signed certificate in certificate chain

javascript - 无法使用 angular 2 指令将带有 Selenium 的文本输入到字段中

python - 在 python 中使用 selenium webdriver 查找具有显式等待的元素

selenium-webdriver - 更改Edge Chrome的默认下载位置

java - 添加带有过期时间的 cookie 时,Cookie 过期时间必须为正整数 最大 java 日期 Aug 17, 292278994 12 :42:55 PM