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);
            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

Contributors