Challenge - 5 Problems
ClassName Locator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of findElement by className with multiple matches
What will be the output of the following Selenium Java code snippet when multiple elements share the same class name?
Selenium Java
WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); WebElement element = driver.findElement(By.className("btn")); System.out.println(element.getText());
Attempts:
2 left
💡 Hint
Remember that findElement returns only the first matching element.
✗ Incorrect
The findElement method returns the first WebElement matching the locator. Even if multiple elements share the class name, only the first is returned and its text printed.
❓ assertion
intermediate2:00remaining
Correct assertion for element found by className
Which assertion correctly verifies that the element found by className "header" is displayed on the page?
Selenium Java
WebElement header = driver.findElement(By.className("header"));Attempts:
2 left
💡 Hint
Check if the element is visible to the user.
✗ Incorrect
The isDisplayed() method returns true if the element is visible. Using assertTrue with isDisplayed() confirms the element is shown on the page.
❓ locator
advanced2:00remaining
Identify invalid className locator usage
Which of the following className locators is invalid and will cause an error in Selenium Java?
Attempts:
2 left
💡 Hint
Class names with spaces are not valid for By.className locator.
✗ Incorrect
The By.className locator accepts only a single class name without spaces. "btn primary" contains a space and will cause an InvalidSelectorException.
🔧 Debug
advanced2:00remaining
Debugging NoSuchElementException with className
Given the code below, why does it throw NoSuchElementException?
Selenium Java
WebElement menu = driver.findElement(By.className("menu-item"));Attempts:
2 left
💡 Hint
Check if the page contains the element with the specified class.
✗ Incorrect
NoSuchElementException occurs when no element matches the locator. If the class 'menu-item' is not present on the page, this exception is thrown.
❓ framework
expert2:00remaining
Best practice for waiting for element by className
Which code snippet correctly waits up to 10 seconds for an element with className "submit-btn" to be visible before interacting with it?
Attempts:
2 left
💡 Hint
Use explicit waits to wait for specific conditions.
✗ Incorrect
Using WebDriverWait with ExpectedConditions.visibilityOfElementLocated waits up to 10 seconds for the element to be visible, avoiding timing issues.