We use findElements to get all web elements that match a locator. This helps when we want to work with many items on a page, not just one.
findElements for multiple matches in Selenium Java
Introduction
Syntax
Selenium Java
List<WebElement> elements = driver.findElements(By.locatorType("locatorValue"));findElements returns a list of elements, even if none are found (empty list).
Use By to specify how to find elements (id, className, xpath, cssSelector, etc.).
Examples
btn.Selenium Java
List<WebElement> buttons = driver.findElements(By.className("btn"));a) tags on the page.Selenium Java
List<WebElement> links = driver.findElements(By.tagName("a"));Selenium Java
List<WebElement> items = driver.findElements(By.xpath("//ul/li"));Sample Program
This program opens a webpage, finds all buttons with class btn, prints how many it found, and prints each button's text.
Selenium Java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.List; public class FindElementsExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/samplepage"); // Find all buttons with class 'btn' List<WebElement> buttons = driver.findElements(By.className("btn")); System.out.println("Number of buttons found: " + buttons.size()); // Print text of each button for (WebElement button : buttons) { System.out.println("Button text: " + button.getText()); } } finally { driver.quit(); } } }
Important Notes
If no elements match, findElements returns an empty list, not an error.
Always quit the driver to close the browser after the test.
Use meaningful locators to avoid finding wrong elements.
Summary
findElements gets all matching elements as a list.
It helps when you want to work with many similar elements.
Returns empty list if no matches, so no crash.
Practice
1. What does the Selenium method
findElements return when multiple elements match the locator?easy
Solution
Step 1: Understand the purpose of findElements
The methodfindElementsis designed to find all elements matching a locator, not just one.Step 2: Check the return type
It returns a list (java.util.List) of WebElements, which can be empty if no matches are found.Final Answer:
A list of all matching WebElements -> Option CQuick Check:
findElements returns list = D [OK]
Hint: Remember: findElements always returns a list, even if empty [OK]
Common Mistakes:
- Confusing findElements with findElement which returns single element
- Expecting an error when multiple matches occur
- Assuming null is returned if no matches
2. Which of the following is the correct syntax to find all buttons on a page using Selenium Java?
easy
Solution
Step 1: Identify correct method and return type
To get multiple elements, usefindElementswhich returns a List<WebElement>.Step 2: Check syntax correctness
List<WebElement> buttons = driver.findElements(By.tagName("button")); correctly uses List<WebElement> and findElements with By.tagName("button").Final Answer:
List<WebElement> buttons = driver.findElements(By.tagName("button")); -> Option AQuick Check:
Multiple elements need findElements + List [OK]
Hint: Use findElements with List<WebElement> for multiple matches [OK]
Common Mistakes:
- Using findElement instead of findElements for multiple elements
- Assigning findElements result to a single WebElement variable
- Mixing findElement with List type
3. What will be the output of the following code snippet?
Assuming the page has 4 elements with class "item".
List<WebElement> items = driver.findElements(By.className("item"));
System.out.println(items.size());Assuming the page has 4 elements with class "item".
medium
Solution
Step 1: Understand findElements behavior
findElements returns a list of all matching elements. Here, 4 elements have class "item".Step 2: Check the size of the list
Callingitems.size()returns the number of elements found, which is 4.Final Answer:
4 -> Option BQuick Check:
items.size() = 4 [OK]
Hint: items.size() gives count of all matched elements [OK]
Common Mistakes:
- Expecting findElements to throw exception if multiple matches
- Confusing findElements with findElement which throws exception if none
- Assuming size() returns 1 always
4. Identify the error in the following code snippet:
Assuming the page has only 3 matching elements.
List<WebElement> links = driver.findElements(By.cssSelector("a.link"));
System.out.println(links.get(5).getText());Assuming the page has only 3 matching elements.
medium
Solution
Step 1: Check findElements result size
findElements returns a list with 3 elements because only 3 match the selector.Step 2: Accessing index 5 causes error
Trying to get element at index 5 (6th element) causes IndexOutOfBoundsException because list size is 3.Final Answer:
IndexOutOfBoundsException at links.get(5) -> Option DQuick Check:
Accessing invalid index = IndexOutOfBoundsException [OK]
Hint: Check list size before accessing by index to avoid errors [OK]
Common Mistakes:
- Expecting NoSuchElementException from findElements when no matches
- Assuming list is null if no matches (it's empty, not null)
- Ignoring list size before get(index)
5. You want to click all buttons with class "submit-btn" on a page. Which code snippet correctly does this using
findElements?hard
Solution
Step 1: Use findElements to get all matching buttons
List<WebElement> buttons = driver.findElements(By.className("submit-btn")); for(WebElement btn : buttons) { btn.click(); } correctly uses findElements to get a List of all buttons with class "submit-btn".Step 2: Iterate over list and click each button
The for-each loop clicks each button in the list, which is the correct way to handle multiple elements.Final Answer:
List<WebElement> buttons = driver.findElements(By.className("submit-btn")); for(WebElement btn : buttons) { btn.click(); } -> Option AQuick Check:
findElements + loop to click all = B [OK]
Hint: Use findElements + loop to act on multiple elements [OK]
Common Mistakes:
- Using findElement which returns only one element
- Trying to call click() directly on list
- Assigning findElements result to single WebElement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
