Wednesday, September 10, 2014

How to continue refreshing a page until the element is loaded

Sometimes we need to continue refreshing the page until an specific element is loaded. In that case we need to write some extra code beside selenium webdriver command.

We can use do…While() loop using the specific element as locator and the function “isDisplayed()” to detect that the element is appeared. We run the loop until we get the element loaded on the page. Every time we execute the refresh() method under the loop.

do{
            // Reload the page

}while(the specific element is not displayed);

In Java,

do{
driver.navigate().refresh();
}while(!driver.findElement(By.id("gbqfq")).isDisplayed());
I think this will help those who are fighting with element loading problem.
Happy Selenium Webdrivering!!!

4 comments:


  1. I have tried using your method in my application.My test case is failing instead of refreshing the page until the element exists.Please help

    ReplyDelete
  2. Please use the appropriate while condition. In my case it was working. You can share the code here if you have not any problem. I am ready to help. Thank you.

    ReplyDelete
  3. I have a scenario. I send an email to my gmail account from application. It takes varying time.
    Now I m trying to search the email with an unique name given in content and refresh it until that text appears in the content , then click on the email. I tried with recursive functions. Please do help me

    ReplyDelete
  4. hello! how to refresh 10 times? do while is an endless loop going to be in my case

    ReplyDelete

Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.   Now it is for cypress. Cypress...