Bird
0
0

Analyze the following Selenium Java code:

medium📝 Predict Output Q5 of 15
Selenium Java - Handling Form Elements
Analyze the following Selenium Java code:
WebElement checkbox = driver.findElement(By.name("newsletter"));
if (!checkbox.isSelected()) {
    checkbox.click();
}
System.out.println(checkbox.isSelected());

What will be the output after execution?
AThrows NoSuchElementException
Bfalse
Cnull
Dtrue
Step-by-Step Solution
Solution:
  1. Step 1: Locate checkbox

    The checkbox is found by name "newsletter".
  2. Step 2: Check if selected

    If not selected, the checkbox is clicked to select it.
  3. Step 3: Print selection state

    After clicking, isSelected() returns true.
  4. Final Answer:

    true -> Option D
  5. Quick Check:

    Checkbox clicked if unchecked, so isSelected() is true [OK]
Quick Trick: Click sets checkbox selected, so isSelected() returns true [OK]
Common Mistakes:
  • Assuming isSelected() remains false after click
  • Confusing locator strategies causing exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes