+2 votes
217 views
in Software Testing by (1.6k points)

is there a way how to test if an element is present? Any findElement method would end in an exception, but that is not what I want, because it can be that an element is not present and that is okay, that is not a fail of the test, so an exception can not be the solution.

 

public static class WebDriverExtensions

{
    public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(drv => drv.FindElement(by));
        }
    return driver.FindElement(by);
    }
}

1 Answer

+1 vote
by (2.6k points)

Use findElements instead of findElement.

findElements will return an empty list if no matching elements are found instead of an exception.

To check that an element is present, you could try this

Boolean isPresent = driver.findElements(By.yourLocator).size()<0

This will return false if the element is not found or true if at least one element is found.

by (1.6k points)
thanks himanshu its works.

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated