Bird
0
0

What is wrong with this code snippet for selecting a radio button?

medium📝 Debug Q7 of 15
Selenium Java - Handling Form Elements
What is wrong with this code snippet for selecting a radio button?
WebElement rb = driver.findElement(By.name("gender"));
if(rb.isSelected()) {
  rb.click();
}
AIt clicks the radio button twice
BIt never clicks the radio button
CIt throws NoSuchElementException
DIt clicks the radio button only if already selected
Step-by-Step Solution
Solution:
  1. Step 1: Analyze if condition logic

    The code clicks the radio button only if it is already selected.
  2. Step 2: Understand expected behavior

    Usually, you want to click if not selected, so logic is reversed.
  3. Final Answer:

    It clicks the radio button only if already selected -> Option D
  4. Quick Check:

    Click only if selected = wrong logic [OK]
Quick Trick: Click radio button only if not selected (use !isSelected()) [OK]
Common Mistakes:
  • Reversing condition logic
  • Clicking radio button unnecessarily
  • Ignoring isSelected() return value meaning

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes