Bird
0
0

Consider this code snippet:

medium📝 Predict Output Q13 of 15
Selenium Java - Handling Form Elements
Consider this code snippet:
WebElement checkbox = driver.findElement(By.id("agree"));
if (!checkbox.isSelected()) {
    checkbox.click();
}
System.out.println(checkbox.isSelected());
What will be printed if the checkbox was initially unchecked?
Afalse
Btrue
Cnull
DThrows exception
Step-by-Step Solution
Solution:
  1. Step 1: Check initial checkbox state

    The checkbox is initially unchecked, so isSelected() returns false.
  2. Step 2: Click checkbox if not selected

    Since it is not selected, checkbox.click() runs, selecting it.
  3. Step 3: Print final state

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

    true -> Option B
  5. Quick Check:

    Unchecked + click = selected = true [OK]
Quick Trick: Click only if not selected to ensure checkbox is checked [OK]
Common Mistakes:
  • Assuming initial state remains unchanged
  • Expecting false after click
  • Confusing isSelected() return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes