Test if element is present using Selenium WebDriver? (Java)

Test if element is present using Selenium WebDriver? (Java)

The easiest way to check if an element is present on a web page is to find all the elements matching the element locator and check that the size of the resulting list is not 0. Selenium doesn't offer any solutions more concise than this due to the nature of element locating.

boolean isPresent = driver.findElements(By.id("searchInput")).size() > 0;

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.