Fluent wait example with code

0 votes
Jun 5, 2018 in Selenium by graci
• 240 points
7,309 views

1 answer to this question.

0 votes

When we try to test the presence of an element that may appear after every x seconds/minutes then comes the need of fluent wait. It tries to find the web element repeatedly at regular intervals(as specified in polling period) of time until the timeout or till the object gets found. It can define the maximum amount of time to wait for a specific condition and frequency with which to check the condition before throwing an “ElementNotVisibleException” exception.  

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

WebElement ele = wait.until(new Function<WebDriver, WebElement>()

{

public WebElement apply(WebDriver driver)

            {         

WebElement element = driver.findElement(By.xpath("html/body/div[2]/div[4]/div[2]/div[1]/h1"));

String getTextOnPage = element.getText();

if(getTextOnPage.equals("Selenium Certification Training"))

{

System.out.println(getTextOnPage);

return element;

}                                             

else

{

System.out.println("FluentWait Failed");

                        return null;

}}

});

Fluent Wait uses two parameters – timeout value and polling frequency. In the above syntax we took time out value as 30 seconds and polling frequency as 5 seconds. The maximum amount of time (30 seconds) to wait for a condition and the frequency (5 seconds) to check the success or failure of a specified condition. If the element is located within this time frame it will perform the operations else it will throw an “ElementNotVisibleException”. So, when we have web elements which sometimes visible in few seconds and some times take more time than usual (Ajax applications). We can set the default pooling period based on our requirement and also ignore any exception while polling an element.

answered Jun 5, 2018 by walter 123
• 240 points

Related Questions In Selenium

0 votes
1 answer

Not able to use “explicit wait” in my code

To wait until the entire data has ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
79 views
0 votes
1 answer
0 votes
1 answer

Using XPath with Selenium-Java code

There is a minor syntax error in ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,650 points
61 views
+4 votes
2 answers

Can we get the HTTP Response Code in Selenium with Java?

It is indeed possible to get http ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,650 points
11,138 views
+1 vote
0 answers
0 votes
2 answers
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
548 views
0 votes
1 answer

Impilicit wait vs Explicit wait vs Fluent wait

Implicit wait: Your telling the WebDriver the ...READ MORE

answered Apr 13, 2018 in Selenium by king_kenny
• 3,650 points
824 views
0 votes
1 answer