Recall & Review
beginner
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.WebElement button = driver.findElement(By.className("button-primary"));
button.click();
Click to reveal answer
intermediate
What is a better alternative locator if you want to find an element with multiple classes?
Use
By.cssSelector with a CSS selector combining classes, e.g., By.cssSelector(".class1.class2").Click to reveal answer
What will happen if you use
findElement(By.className("btn primary")) in Selenium?✗ Incorrect
By.className accepts only a single class name without spaces. Using multiple classes separated by spaces causes an error.
Which Selenium method is best to find an element by a single CSS class?
✗ Incorrect
By.className is designed to find elements by a single CSS class.
If
findElement(By.className("menu")) finds multiple elements, which one is returned?✗ Incorrect
findElement returns the first matching element found in the DOM.
What exception does Selenium throw if
findElement(By.className()) finds no element?✗ Incorrect
NoSuchElementException is thrown when no matching element is found.
Which locator is better to find an element with multiple classes 'btn' and 'active'?
✗ Incorrect
By.cssSelector with combined classes is the correct way to find elements with multiple classes.
Explain how to use
findElement(By.className()) in Selenium and mention its limitations.Think about how CSS classes work and Selenium's locator rules.
You got /4 concepts.
Describe how you would handle a situation where
findElement(By.className()) does not find any element during test execution.Consider exception handling best practices in Selenium.
You got /4 concepts.