I am working with Selenium Standalone Server 3.0.1. I am trying to add an Explicit Wait to my code to detect an element through xpath when the element becomes visible. In order to get some Java help I looked out for the source code for Selenium Standalone Server 3.0.1 but was unable to find it. I found the source code in selenium-java-2.53.1 release. I downloaded it and found selenium-java-2.53.1-srcs and added to my Eclipse IDE. From the help of FluentWait, I simply copy pasted the code in my Eclipse IDE and changed the variable names.

The sample code in documentation is like:

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

But when I implement this code, simply copy pasting it:

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

I am getting an error on FluentWait Class as The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver>

Here is the list of my imports:

    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;

Can anyone help me out please?


Update

Added an answer with respect to the modified constructor of FluentWait in Selenium v3.11.0

6

There are 6 best solutions below

6
On BEST ANSWER

You need to specify expected condition inside the wait below is the modified code that could solve your problem.

Code:

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

The simplest solution is to use the other method implementation:

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

The form withTimeout(Duration timeOut) is still used and non-deprecated one

2
On

I was also facing the same error, later I noticed that I was using the class name as fluentwait. After changing the class name it was working fine.

0
On

I got the same error since I named the class 'FluentWait' that I created in eclipse to implement fluent wait. Try renaming the class to a different name.

1
On

With the availability of Selenium v3.11.0 the constructor of FluentWait have changed. Now the argument type for withTimeout and pollingEvery is Duration. Here is the modified implementation :

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

    }

}
0
On

I was also facing same error that The type FluentWait is not generic; it cannot be parameterized with arguments <WebDriver> but i noticed a silly mistake that my main class was also named as FuentWait and it was not generic. I changed its name and error went away.