Bird
0
0

You want to ensure a checkbox with ID "terms" is selected before submitting a form. Which code snippet correctly handles this and avoids unnecessary clicks?

hard📝 Application Q15 of 15
Selenium Java - Handling Form Elements
You want to ensure a checkbox with ID "terms" is selected before submitting a form. Which code snippet correctly handles this and avoids unnecessary clicks?
AWebElement cb = driver.findElement(By.id("terms")); if (!cb.isSelected()) { cb.click(); } // proceed to submit
BWebElement cb = driver.findElement(By.id("terms")); cb.click(); // proceed to submit
CWebElement cb = driver.findElement(By.id("terms")); if (cb.isSelected()) { cb.click(); } // proceed to submit
DWebElement cb = driver.findElement(By.id("terms")); cb.isSelected(); // proceed to submit
Step-by-Step Solution
Solution:
  1. Step 1: Check checkbox state before clicking

    Use isSelected() to verify if checkbox is already selected.
  2. Step 2: Click only if not selected

    Click checkbox only when it is not selected to avoid toggling off.
  3. Step 3: Avoid unnecessary clicks

    WebElement cb = driver.findElement(By.id("terms")); if (!cb.isSelected()) { cb.click(); } // proceed to submit correctly implements this logic, ensuring checkbox is selected before submit.
  4. Final Answer:

    Code that clicks checkbox only if not selected before submit. -> Option A
  5. Quick Check:

    Check state, click if false, then submit [OK]
Quick Trick: Click checkbox only if not selected to ensure correct state [OK]
Common Mistakes:
  • Clicking checkbox unconditionally causing toggle off
  • Clicking checkbox when already selected
  • Ignoring checkbox state before submit

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes