Challenge - 5 Problems
FindElement by Name Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of findElement by name with multiple matching elements
What will be the output of the following Selenium Java code snippet when multiple elements share the same name attribute?
Selenium Java
WebDriver driver = new ChromeDriver(); driver.get("https://example.com/form"); WebElement element = driver.findElement(By.name("username")); System.out.println(element.getTagName());
Attempts:
2 left
💡 Hint
findElement returns the first matching element, not all elements.
✗ Incorrect
The findElement method returns the first WebElement matching the locator. Even if multiple elements share the same name, only the first is returned. No exception is thrown unless no elements match.
❓ assertion
intermediate1:30remaining
Correct assertion for element found by name
Which assertion correctly verifies that the element found by name 'email' is displayed on the page?
Selenium Java
WebElement emailField = driver.findElement(By.name("email"));Attempts:
2 left
💡 Hint
To check visibility, use isDisplayed() method.
✗ Incorrect
The isDisplayed() method returns true if the element is visible on the page. The assertion assertTrue(emailField.isDisplayed()) correctly verifies this.
🔧 Debug
advanced2:30remaining
Debugging NoSuchElementException with findElement by name
Given the code below, why does it throw NoSuchElementException?
Selenium Java
driver.get("https://example.com/login"); WebElement password = driver.findElement(By.name("pass"));
Attempts:
2 left
💡 Hint
Check the actual HTML for the name attribute value.
✗ Incorrect
NoSuchElementException occurs when no element matches the locator. If the page has no element with name 'pass', this exception is thrown.
🧠 Conceptual
advanced2:00remaining
Behavior of findElement vs findElements by name
Which statement correctly describes the difference between findElement(By.name("user")) and findElements(By.name("user"))?
Attempts:
2 left
💡 Hint
One returns a single element, the other returns a list.
✗ Incorrect
findElement returns the first matching element or throws NoSuchElementException if none found. findElements returns a list of all matching elements or an empty list if none found.
❓ framework
expert3:00remaining
Best practice for waiting for element by name before interaction
In Selenium Java, which code snippet correctly waits up to 10 seconds for an element with name 'submit' to be clickable before clicking it?
Attempts:
2 left
💡 Hint
Explicit waits are better than implicit waits or sleep for waiting specific conditions.
✗ Incorrect
Using WebDriverWait with ExpectedConditions.elementToBeClickable waits up to the timeout for the element to be clickable, then clicks it. This is the best practice for reliable interaction.