Fluent wait example with code

0 votes
Jun 5, 2018 in Selenium by graci
• 240 points
13,799 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

+1 vote
2 answers
0 votes
1 answer

Not able to use “explicit wait” in my code

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

answered Mar 28, 2018 in Selenium by nsv999
• 5,500 points
1,167 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 14, 2018 in Selenium by king_kenny
• 3,710 points
971 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 21, 2018 in Selenium by king_kenny
• 3,710 points
41,490 views
0 votes
0 answers

Could you please help me with some selenium projects along with source code?

I am learning selenium and  I have ...READ MORE

Mar 10, 2019 in Selenium by Mark
717 views
+1 vote
4 answers

Need to wait until page is completely loaded - Selenium WebDriver

You can try something like -  new WebDriverWait(firefoxDriver, ...READ MORE

answered Dec 21, 2019 in Selenium by Robin
64,144 views
0 votes
2 answers

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,500 points
13,494 views
0 votes
1 answer

Impilicit wait vs Explicit wait vs Fluent wait

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

answered Apr 14, 2018 in Selenium by king_kenny
• 3,710 points
4,287 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,031 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP