Bird
0
0

Identify the error in the following Selenium Java code for checking a checkbox:

medium📝 Debug Q6 of 15
Selenium Java - Handling Form Elements
Identify the error in the following Selenium Java code for checking a checkbox:
WebElement checkbox = driver.findElement(By.id("terms"));
if (checkbox.isSelected() = false) {
    checkbox.click();
}
AUsing assignment operator '=' instead of comparison '==' in if condition
BMissing semicolon after findElement statement
CUsing click() instead of select() method
DIncorrect locator type used
Step-by-Step Solution
Solution:
  1. Step 1: Check if condition syntax

    The condition uses '=' which assigns value instead of '==' for comparison.
  2. Step 2: Correct syntax

    It should be checkbox.isSelected() == false or better !checkbox.isSelected().
  3. Final Answer:

    Using assignment operator '=' instead of comparison '==' in if condition -> Option A
  4. Quick Check:

    Use '==' for comparison, not '=' [OK]
Quick Trick: Use '==' for comparison in if conditions [OK]
Common Mistakes:
MISTAKES
  • Using '=' instead of '==' in conditions
  • Confusing click() with select() method
  • Ignoring syntax errors in if statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes