Bird
Raised Fist0
Selenium Javatesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Selenium method findElement(By.className("btn-primary")) do in a test script?
easy
A. It finds an element by its HTML tag name "btn-primary".
B. It finds all web elements with the CSS class "btn-primary" on the page.
C. It finds the first web element with the CSS class "btn-primary" on the page.
D. It finds an element by its ID "btn-primary".

Solution

  1. Step 1: Understand the method purpose

    The method findElement locates a single element on the page.
  2. Step 2: Analyze the locator type

    Using By.className("btn-primary") targets elements with the CSS class "btn-primary".
  3. Final Answer:

    It finds the first web element with the CSS class "btn-primary" on the page. -> Option C
  4. Quick Check:

    findElement + className = first matching element [OK]
Hint: findElement with className returns first matching element [OK]
Common Mistakes:
  • Confusing findElement with findElements (plural)
  • Using className to find by ID or tag
  • Expecting multiple elements returned
2. Which of the following is the correct syntax to find an element by class name "login-button" in Selenium Java?
easy
A. driver.findElement(By.classname("login-button"));
B. driver.findElement(By.className("login-button"));
C. driver.findElement(By.class_name("login-button"));
D. driver.findElement(By.class("login-button"));

Solution

  1. Step 1: Recall correct method usage

    The correct method to locate by class name is By.className() with capital N.
  2. Step 2: Check syntax correctness

    driver.findElement(By.className("login-button")); uses the exact correct syntax: By.className("login-button").
  3. Final Answer:

    driver.findElement(By.className("login-button")); -> Option B
  4. Quick Check:

    Correct method name is className with capital N [OK]
Hint: Use By.className with capital N for correct syntax [OK]
Common Mistakes:
  • Using By.class instead of By.className
  • Wrong method casing like class_name or classname
  • Missing quotes around class name
3. Given the HTML snippet:
<button class="submit-btn primary">Submit</button>

What will the following code return?
WebElement btn = driver.findElement(By.className("primary"));
System.out.println(btn.getText());
medium
A. Submit
B. primary
C. submit-btn
D. NoSuchElementException

Solution

  1. Step 1: Locate element by class name "primary"

    The button has class "primary" among others, so it matches.
  2. Step 2: Get the text of the found element

    The getText() method returns the visible text inside the button, which is "Submit".
  3. Final Answer:

    Submit -> Option A
  4. Quick Check:

    findElement by className finds element, getText returns inner text [OK]
Hint: getText() returns visible text inside found element [OK]
Common Mistakes:
  • Expecting class name as text output
  • Confusing class attribute with element text
  • Assuming exception if multiple classes present
4. What is wrong with this Selenium Java code snippet?
WebElement element = driver.findElement(By.className("btn primary"));

It tries to find an element with classes "btn" and "primary".
medium
A. The syntax is correct; it finds elements with both classes.
B. It should use findElements instead of findElement.
C. It should use By.id instead of By.className.
D. className locator cannot contain spaces; it matches only one class.

Solution

  1. Step 1: Understand className locator rules

    The By.className locator accepts only a single class name without spaces.
  2. Step 2: Identify the error in locator usage

    Using "btn primary" with space is invalid and causes an error; it cannot find elements with multiple classes this way.
  3. Final Answer:

    className locator cannot contain spaces; it matches only one class. -> Option D
  4. Quick Check:

    className accepts single class only, no spaces [OK]
Hint: Use single class name without spaces in className locator [OK]
Common Mistakes:
  • Trying to find multiple classes with space-separated string
  • Expecting findElement to accept compound class selectors
  • Confusing className with CSS selector syntax
5. You want to click the first button with class "nav-item active" on a page. Which is the best way to do this using Selenium Java?
hard
A. driver.findElement(By.cssSelector(".nav-item.active")).click();
B. driver.findElement(By.className("nav-item active")).click();
C. driver.findElement(By.className("nav-item")).click();
D. driver.findElement(By.xpath("//button[@class='nav-item active']")).click();

Solution

  1. Step 1: Understand limitation of className locator

    The By.className locator cannot handle multiple classes separated by spaces.
  2. Step 2: Use CSS selector for multiple classes

    The CSS selector .nav-item.active matches elements with both classes and works correctly.
  3. Step 3: Compare other options

    driver.findElement(By.className("nav-item active")).click(); is invalid due to space in className; driver.findElement(By.className("nav-item")).click(); matches only one class; driver.findElement(By.xpath("//button[@class='nav-item active']")).click(); uses XPath but is less concise.
  4. Final Answer:

    driver.findElement(By.cssSelector(".nav-item.active")).click(); -> Option A
  5. Quick Check:

    Use CSS selector for multiple classes, not className [OK]
Hint: Use CSS selector for multiple classes, not className [OK]
Common Mistakes:
  • Using className with multiple classes separated by space
  • Using only one class when both are needed
  • Ignoring CSS selectors as a better alternative
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard