What if you could test every button on a page in seconds instead of hours?
Why findElements for multiple matches in Selenium Java? - Purpose & Use Cases
Imagine you are testing a webpage with many buttons labeled 'Add to Cart'. You want to check all these buttons to ensure they work correctly. Doing this by clicking each button one by one manually is tiring and slow.
Manually finding and testing each button means you might miss some, make mistakes, or spend hours repeating the same steps. It is easy to lose track and hard to be sure you covered all buttons.
Using findElements lets you collect all matching buttons at once in a list. You can then loop through this list to test each button automatically, saving time and avoiding errors.
driver.findElement(By.id("button1")).click(); driver.findElement(By.id("button2")).click(); // Repeat for each button manually
List<WebElement> buttons = driver.findElements(By.className("add-to-cart")); for (WebElement button : buttons) { button.click(); }
This makes it easy to handle many similar elements at once, enabling faster and more reliable testing of complex pages.
Testing an online store where multiple products have 'Add to Cart' buttons, ensuring all buttons respond correctly without missing any.
Manual testing of many elements is slow and error-prone.
findElements collects all matching elements in one step.
Looping through the list automates repetitive checks efficiently.
Practice
findElements return when multiple elements match the locator?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]
- Confusing findElements with findElement which returns single element
- Expecting an error when multiple matches occur
- Assuming null is returned if no matches
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]
- Using findElement instead of findElements for multiple elements
- Assigning findElements result to a single WebElement variable
- Mixing findElement with List type
List<WebElement> items = driver.findElements(By.className("item"));
System.out.println(items.size());Assuming the page has 4 elements with class "item".
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]
- Expecting findElements to throw exception if multiple matches
- Confusing findElements with findElement which throws exception if none
- Assuming size() returns 1 always
List<WebElement> links = driver.findElements(By.cssSelector("a.link"));
System.out.println(links.get(5).getText());Assuming the page has only 3 matching elements.
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]
- 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)
findElements?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]
- Using findElement which returns only one element
- Trying to call click() directly on list
- Assigning findElements result to single WebElement
Ready to prove it?
Pass the test and this pattern moves unknown
