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>
这是我的进口 list :
    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

最佳答案

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

码:

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

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

相关文章:

java - 对象死亡时不执行终结器方法

angularjs - Protractor E2E测试错误: Cannot find module 'selenium-webdriver'

java - 使用 java selenium 从 img src 获取链接

java - RemoteWebDriver(Firefox) 不适用于 xpath

python - Selenium 源缺少登录字段

php - 如何在使用 PHPUnit、Selenium 和 php-webdriver 进行测试后保持登录状态

java - 缺少名称,处于状态 : START_OBJECT parsing XML using Jackson

java - Spring 社交 super 慢,有什么想法吗?

java - 是否可以将 Mockito mock 配置为在一次调用中调用多个答案?

python - 如何使用selenium下载文件?