Challenge - 5 Problems
Master of findElements
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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());Attempts:
2 left
💡 Hint
Remember that findElements returns a list of all matching elements or an empty list if none found.
✗ Incorrect
The findElements method returns a list of all matching elements. If no elements match, it returns an empty list with size 0. It does not throw an exception.
❓ assertion
intermediate2: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"));Attempts:
2 left
💡 Hint
You want to check if there are three or more elements, not exactly three.
✗ Incorrect
assertTrue(inputs.size() >= 3) checks that the list has at least 3 elements. assertEquals(inputs.size(), 3) would fail if more than 3 exist. assertFalse(inputs.isEmpty()) only checks if there is at least one element. assertNull is invalid because findElements never returns null.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Think about the difference in behavior between findElement and findElements.
✗ Incorrect
findElements returns a list of matching elements or an empty list if none found. It does not throw exceptions for no matches. findElement throws NoSuchElementException if no element is found.
❓ locator
advanced2: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?Attempts:
2 left
💡 Hint
Attribute selectors in CSS use square brackets.
✗ Incorrect
By.cssSelector("[data-test='value']") correctly selects elements with attribute data-test equal to value. The other locators do not select by attribute values.
❓ framework
expert3: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?
Attempts:
2 left
💡 Hint
Think about how page updates affect element references.
✗ Incorrect
StaleElementReferenceException occurs when the page changes and previously found elements are no longer valid. Refetching elements with findElements ensures fresh references.