一尘不染

FluentWait类型不是通用的;不能使用参数对其进行参数化 Selenium和Java导致FluentWait类错误

selenium

我正在与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']"));
         }
       });

我在FluentWaitClass 上遇到错误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;

有人可以帮我吗?


更新资料

增加了一个回答关于的修改构造 FluentWaitseleniumV3.11.0


阅读 285

收藏
2020-06-26

共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"));
            }
        }
    }
}
2020-06-26