0
0
Selenium Javatesting~20 mins

findElement by name in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FindElement by Name Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
AThrows StaleElementReferenceException immediately
BThrows NoSuchElementException because multiple elements have the same name
CPrints the tag name of all elements with name 'username'
DPrints the tag name of the first element with name 'username', e.g., 'input'
Attempts:
2 left
💡 Hint
findElement returns the first matching element, not all elements.
assertion
intermediate
1: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"));
AassertNull(emailField);
BassertEquals(emailField.getText(), "email");
CassertTrue(emailField.isDisplayed());
DassertFalse(emailField.isEnabled());
Attempts:
2 left
💡 Hint
To check visibility, use isDisplayed() method.
🔧 Debug
advanced
2: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"));
AThe driver is not initialized properly
BThe page does not have any element with name attribute 'pass'
CThe findElement method requires an ID locator, not name
DThe element is inside an iframe and not switched to it
Attempts:
2 left
💡 Hint
Check the actual HTML for the name attribute value.
🧠 Conceptual
advanced
2:00remaining
Behavior of findElement vs findElements by name
Which statement correctly describes the difference between findElement(By.name("user")) and findElements(By.name("user"))?
AfindElement returns the first matching element or throws exception; findElements returns a list of all matching elements or empty list
BfindElement returns a list of elements; findElements returns only the first element
CBoth methods return the same result but findElements is slower
DfindElement returns null if no element found; findElements throws exception
Attempts:
2 left
💡 Hint
One returns a single element, the other returns a list.
framework
expert
3: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?
Anew WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.name("submit"))).click();
BThread.sleep(10000); driver.findElement(By.name("submit")).click();
Cdriver.findElement(By.name("submit")).click();
Ddriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElement(By.name("submit")).click();
Attempts:
2 left
💡 Hint
Explicit waits are better than implicit waits or sleep for waiting specific conditions.