Bird
Raised Fist0
Selenium Javatesting~20 mins

findElements for multiple matches in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of findElements
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the size of the list returned by findElements?
Consider the following Selenium Java code snippet. What will be the size of the list elements after execution?
Selenium Java
List<WebElement> elements = driver.findElements(By.className("btn"));
System.out.println(elements.size());
AThe number of elements with class 'btn' on the current page
B0
C1
DThrows NoSuchElementException
Attempts:
2 left
💡 Hint
Remember that findElements returns a list of all matching elements or an empty list if none found.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies multiple elements are found?
You want to assert that at least 3 elements with tag name 'input' are present on the page. Which assertion is correct?
Selenium Java
List<WebElement> inputs = driver.findElements(By.tagName("input"));
AassertNull(inputs);
BassertTrue(inputs.size() >= 3);
CassertFalse(inputs.isEmpty());
DassertEquals(inputs.size(), 3);
Attempts:
2 left
💡 Hint
You want to check if there are three or more elements, not exactly three.
🔧 Debug
advanced
2:00remaining
Why does findElements return an empty list instead of throwing an exception?
You wrote code using findElements but it returns an empty list when no elements match. Why does it not throw NoSuchElementException like findElement?
ABecause the locator syntax is incorrect and silently ignored
BBecause findElements always returns null when no matches
CBecause the driver is not initialized properly
DBecause findElements is designed to return zero or more elements safely without exceptions
Attempts:
2 left
💡 Hint
Think about the difference in behavior between findElement and findElements.
❓ locator
advanced
2:00remaining
Which locator finds multiple elements with attribute data-test='value'?
You want to find all elements with attribute data-test equal to value. Which locator is correct?
ABy.name("data-test='value'")
BBy.id("data-test='value'")
CBy.cssSelector("[data-test='value']")
DBy.className("data-test='value'")
Attempts:
2 left
💡 Hint
Attribute selectors in CSS use square brackets.
❓ framework
expert
3:00remaining
How to handle stale element references when using findElements?
You use findElements to get multiple elements, but sometimes get StaleElementReferenceException when interacting with them. What is the best approach to handle this?
ARefetch the elements using findElements again before interacting with them
BCatch the exception and ignore it without retrying
CStore the elements once and reuse them without refetching
DUse findElement instead of findElements to avoid stale references
Attempts:
2 left
💡 Hint
Think about how page updates affect element references.

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

  1. Step 1: Understand the purpose of findElements

    The method findElements is designed to find all elements matching a locator, not just one.
  2. Step 2: Check the return type

    It returns a list (java.util.List) of WebElements, which can be empty if no matches are found.
  3. Final Answer:

    A list of all matching WebElements -> Option C
  4. 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

  1. Step 1: Identify correct method and return type

    To get multiple elements, use findElements which returns a List<WebElement>.
  2. Step 2: Check syntax correctness

    List<WebElement> buttons = driver.findElements(By.tagName("button")); correctly uses List<WebElement> and findElements with By.tagName("button").
  3. Final Answer:

    List<WebElement> buttons = driver.findElements(By.tagName("button")); -> Option A
  4. 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?
List<WebElement> items = driver.findElements(By.className("item"));
System.out.println(items.size());

Assuming the page has 4 elements with class "item".
medium
A. 1
B. 4
C. 0
D. Throws NoSuchElementException

Solution

  1. Step 1: Understand findElements behavior

    findElements returns a list of all matching elements. Here, 4 elements have class "item".
  2. Step 2: Check the size of the list

    Calling items.size() returns the number of elements found, which is 4.
  3. Final Answer:

    4 -> Option B
  4. Quick 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:
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
A. Syntax error in CSS selector
B. NoSuchElementException thrown by findElements
C. NullPointerException because links is null
D. IndexOutOfBoundsException at links.get(5)

Solution

  1. Step 1: Check findElements result size

    findElements returns a list with 3 elements because only 3 match the selector.
  2. Step 2: Accessing index 5 causes error

    Trying to get element at index 5 (6th element) causes IndexOutOfBoundsException because list size is 3.
  3. Final Answer:

    IndexOutOfBoundsException at links.get(5) -> Option D
  4. Quick 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
A. List<WebElement> buttons = driver.findElements(By.className("submit-btn")); for(WebElement btn : buttons) { btn.click(); }
B. WebElement button = driver.findElement(By.className("submit-btn")); button.click();
C. List<WebElement> buttons = driver.findElement(By.className("submit-btn")); for(WebElement btn : buttons) { btn.click(); }
D. driver.findElements(By.className("submit-btn")).click();

Solution

  1. 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".
  2. 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.
  3. Final Answer:

    List<WebElement> buttons = driver.findElements(By.className("submit-btn")); for(WebElement btn : buttons) { btn.click(); } -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard