Hey there! any question in your mind? Ask It Now!.

Popular Categories

Selenium WebDriver using this test if element is present

+2 votes
asked in Software Testing by tester (1,550 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
answered by Bhartesh (2,410 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.

commented by tester (1,550 points)
thanks himanshu its works.

Related Questions

+1 vote
1 answer 309 views

Not a Member yet?

Ask to Folks Login

My Account
263 Folks are online
0 members and 263 guest online
Your feedback is highly appreciated