What does findElement(By.className("className")) do in Selenium?
It finds the first web element on the page that has the specified CSS class name.
Click to reveal answer
intermediate
Why should you avoid using findElement(By.className()) with multiple classes separated by spaces?
Because <code>By.className</code> accepts only a single class name, not multiple classes separated by spaces. Using multiple classes will cause an error.
Click to reveal answer
beginner
How do you handle the situation if findElement(By.className()) does not find any matching element?
It throws a NoSuchElementException. You can handle it by using try-catch or by checking if elements exist before accessing.
Click to reveal answer
beginner
Write a simple Java code snippet using Selenium to find an element by class name "button-primary" and click it.
The button has class "primary" among others, so it matches.
Step 2: Get the text of the found element
The getText() method returns the visible text inside the button, which is "Submit".
Final Answer:
Submit -> Option A
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
Step 1: Understand className locator rules
The By.className locator accepts only a single class name without spaces.
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.
Final Answer:
className locator cannot contain spaces; it matches only one class. -> Option D
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
Step 1: Understand limitation of className locator
The By.className locator cannot handle multiple classes separated by spaces.
Step 2: Use CSS selector for multiple classes
The CSS selector .nav-item.active matches elements with both classes and works correctly.
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.
Final Answer:
driver.findElement(By.cssSelector(".nav-item.active")).click(); -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.