How can I check if an element exists with Selenium WebDriver?

java, selenium-webdriver, testing, webdriver

Solution

You could alternatively do:

driver.findElements(By.id("...")).size() != 0

Which saves the nasty try/catch

P.S.:

Or more precisely by @JanHrcek here

!driver.findElements(By.id("...")).isEmpty()

Problem

How can I check if an element exist with web driver? Is using a try-catch really the only possible way? ``` boolean present; try { driver.findElement(By.id("logoutLink")); present = true; } catch (NoSuchElementException e) { present = false; } ```

Original source

Related problems