What does the findElements method return in Selenium Java?
It returns a List<WebElement> containing all matching elements found on the page. If no elements match, it returns an empty list.
Click to reveal answer
beginner
How is findElements different from findElement?
findElement returns the first matching element or throws an exception if none found. findElements returns all matching elements as a list and does not throw an exception if none found.
Click to reveal answer
beginner
Why is it useful to use findElements when testing a list of items on a page?
Because it lets you get all items at once, so you can check how many there are, verify their text, or interact with each one in a loop, just like counting apples in a basket.
Click to reveal answer
beginner
What happens if findElements finds no matching elements?
It returns an empty list instead of throwing an error. This helps avoid test failures when elements are optional or temporarily missing.
Click to reveal answer
beginner
Show a simple Java code snippet using findElements to count buttons on a page.
List<WebElement> buttons = driver.findElements(By.tagName("button"));
int count = buttons.size();
System.out.println("Number of buttons: " + count);
Click to reveal answer
What does findElements(By.cssSelector(".item")) return if there are 3 elements with class 'item'?
AA list with 3 WebElements
BThe first WebElement only
CAn exception is thrown
DNull
✗ Incorrect
findElements returns a list of all matching elements. Here, it returns a list with 3 WebElements.
If findElements finds no matches, what is returned?
AAn empty list
BNull
CThrows NoSuchElementException
DReturns the first element anyway
✗ Incorrect
findElements returns an empty list if no elements match, avoiding exceptions.
Which method throws an exception if no element is found?
ANeither
BfindElements
CBoth findElement and findElements
DfindElement
✗ Incorrect
findElement throws NoSuchElementException if no element is found; findElements returns empty list.
How can you check how many elements matched using findElements?
AUse getCount() method
BUse length property
CUse the size() method on the returned list
DUse isEmpty() method only
✗ Incorrect
The size() method on the List tells how many elements were found.
Why might you prefer findElements over findElement when testing multiple similar elements?
ABecause findElement is slower
BTo get all matching elements at once
CBecause findElements throws exceptions
DBecause findElement returns a list
✗ Incorrect
findElements returns all matching elements, useful for testing multiple items.
Explain how you would use findElements in Selenium Java to verify the number of links on a webpage.
Think about how to get all <a> tags and count them.
You got /4 concepts.
Describe the difference in behavior between findElement and findElements when no matching elements are found.
Consider what happens if the element is missing.
You got /3 concepts.
Practice
(1/5)
1. What does the Selenium method findElements return when multiple elements match the locator?
easy
A. A single WebElement or null if none found
B. Only the first matching WebElement
C. A list of all matching WebElements
D. An error if more than one element matches
Solution
Step 1: Understand the purpose of findElements
The method findElements is 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 C
Quick 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
A. List<WebElement> buttons = driver.findElements(By.tagName("button"));
B. WebElement buttons = driver.findElements(By.tagName("button"));
C. List<WebElement> buttons = driver.findElement(By.tagName("button"));
D. WebElement buttons = driver.findElement(By.tagName("button"));
Solution
Step 1: Identify correct method and return type
To get multiple elements, use findElements which 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 A
Quick 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?
D. driver.findElements(By.className("submit-btn")).click();
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.