0
0
Selenium Javatesting~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ClassName Locator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
AThrows NoSuchElementException if more than one element has class 'btn'
BPrints the text of the first element with class 'btn' on the page
CThrows InvalidSelectorException because className cannot be used with multiple elements
DPrints the text of all elements with class 'btn' concatenated
Attempts:
2 left
💡 Hint
Remember that findElement returns only the first matching element.
assertion
intermediate
2: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"));
AassertNotNull(driver.findElement(By.className("header")));
BassertEquals(header.getText(), "header");
CassertTrue(header.isDisplayed());
DassertFalse(header.isEnabled());
Attempts:
2 left
💡 Hint
Check if the element is visible to the user.
locator
advanced
2:00remaining
Identify invalid className locator usage
Which of the following className locators is invalid and will cause an error in Selenium Java?
ABy.className("btn-primary")
BBy.className("nav-item")
CBy.className("header")
DBy.className("btn primary")
Attempts:
2 left
💡 Hint
Class names with spaces are not valid for By.className locator.
🔧 Debug
advanced
2:00remaining
Debugging NoSuchElementException with className
Given the code below, why does it throw NoSuchElementException?
Selenium Java
WebElement menu = driver.findElement(By.className("menu-item"));
AThe element with class 'menu-item' does not exist on the current page
BThe driver was not initialized before calling findElement
CThe className locator cannot find elements with hyphens in class names
DThe findElement method requires an XPath locator, not className
Attempts:
2 left
💡 Hint
Check if the page contains the element with the specified class.
framework
expert
2: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?
Anew WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.className("submit-btn")));
BThread.sleep(10000); driver.findElement(By.className("submit-btn"));
Cdriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); driver.findElement(By.className("submit-btn"));
Ddriver.findElement(By.className("submit-btn")).isDisplayed();
Attempts:
2 left
💡 Hint
Use explicit waits to wait for specific conditions.