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

Popular Categories





Selenium WebDriver using this test if element is present

+2 votes
108 views
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,730 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

+2 votes
1 answer 295 views
+13 votes
1 answer 126 views
+2 votes
1 answer 148 views
+2 votes
1 answer 2,635 views
+1 vote
1 answer 171 views

Not a Member yet?

Ask to Folks Login

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