Friday, 17 June 2016

Check if element really exists in Selenium

Below code demonstrates how to check if an element exists. The code returns true if element exists. It returns false if element is not found. Please note that code will not throw the exception if element is not present.

public boolean checkElementExists(By by, int seconds){
        boolean result=false;
        try {
            driver.manage().timeouts().implicitlyWait(seconds, TimeUnit.SECONDS);
            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

No comments:

Post a Comment

Buy Best Selenium Books