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;
                 else
                     sleep(1000)
                }
                    return false;
        }
        else
        {
            return false;
        }
    }


public boolean checkElementExists(By by, int seconds){
        boolean result=false;
        try {
            driver.manage().timeouts().
implicitlyWait(seconds, TimeUnit.SECONDS);
            driver.findElement(by);
            result = true;
        }catch (org.openqa.selenium.NoSuchElementException ex){
            result = false;
        }
        finally {
            driver.manage().timeouts().
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 reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

About Me

My photo

Self-motivated software professional