Friday, 17 June 2016

Wait until element is displayed in Selenium

Below example illustrates how to wait until element is displayed in Selenium. Please note that we are not using any wait conditions. Below methods are useful when handling Ajax requests. Ajax gets the data from server and displays it on the page dynamically. So we can use below method until element is displayed.

public boolean waitUntilElementIsDisplayed(By by, int seconds){
        if (checkElementExists(by,seconds)){
            for (int i=0;i<seconds;i++){
                 if (driver.findElement(by).isDisplayed())
                     return true;
                    return false;
            return false;

public boolean checkElementExists(By by, int seconds){
        boolean result=false;
        try {
implicitlyWait(seconds, TimeUnit.SECONDS);
            result = true;
        }catch (org.openqa.selenium.NoSuchElementException ex){
            result = false;
        finally {
implicitlyWait(20, TimeUnit.SECONDS);
} return result; }

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at

No comments:

Post a Comment

Buy Best Selenium Books

About Me

My photo

Self-motivated software professional