Recall & Review
beginner
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'?✗ 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?✗ Incorrect
findElements returns an empty list if no elements match, avoiding exceptions.
Which method throws an exception if no element is found?
✗ Incorrect
findElement throws NoSuchElementException if no element is found; findElements returns empty list.
How can you check how many elements matched using findElements?
✗ 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?
✗ 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.