findElement() — returns the first matching element:
Java
WebElement textbox = driver.findElement(By.id("textBoxLocator"));
Throws NoSuchElementException if no element matches.
findElements() — returns all matching elements as a List:
Java
List<WebElement> elements = driver.findElements(By.id("value"));
Returns an empty List (size 0) if no elements match — never throws.
Full comparison:
| Feature | findElement() | findElements() |
|---|---|---|
| Return type | WebElement | List<WebElement> |
| If not found | Throws NoSuchElementException | Returns empty list |
| Multiple matches | Returns FIRST match | Returns ALL matches |
| Use case | Unique elements | Tables, lists, repeated items |
Use findElements() to safely check if element exists:
Java
List<WebElement> list = driver.findElements(By.id("loginBtn"));
if (!list.isEmpty()) {
list.get(0).click();
}
Count elements on page:
Java
int count = driver.findElements(By.cssSelector(".product-card")).size();
System.out.println("Products found: " + count);
