Bird
0
0

You wrote this code to select a checkbox:

medium📝 Debug Q14 of 15
Selenium Java - Handling Form Elements
You wrote this code to select a checkbox:
WebElement cb = driver.findElement(By.id("option1"));
if (cb.isSelected()) {
    cb.click();
}
But the checkbox remains unselected after running. What is the error?
AThe condition should check if checkbox is NOT selected before clicking.
BYou should use <code>cb.select()</code> instead of <code>cb.click()</code>.
CThe locator By.id("option1") is incorrect.
DYou need to use <code>cb.isEnabled()</code> before clicking.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition logic

    The code clicks the checkbox only if it is already selected, which unchecks it.
  2. Step 2: Understand desired action

    To select the checkbox, click it only if it is NOT selected.
  3. Step 3: Correct the condition

    Change condition to if (!cb.isSelected()) { cb.click(); } to select checkbox.
  4. Final Answer:

    The condition should check if checkbox is NOT selected before clicking. -> Option A
  5. Quick Check:

    Click only if not selected to select [OK]
Quick Trick: Click checkbox only if it is not selected [OK]
Common Mistakes:
MISTAKES
  • Clicking checkbox when already selected
  • Using non-existent select() method
  • Ignoring checkbox state before clicking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes