Bird
0
0

You have multiple radio buttons with the same name attribute "color". How do you select the radio button with value "blue" using Selenium WebDriver in Java?

hard📝 Application Q8 of 15
Selenium Java - Handling Form Elements
You have multiple radio buttons with the same name attribute "color". How do you select the radio button with value "blue" using Selenium WebDriver in Java?
Adriver.findElement(By.xpath("//input[@name='blue']")).click();
Bdriver.findElement(By.cssSelector("input[name='color'][value='blue']")).click();
Cdriver.findElement(By.name("color")).click();
Ddriver.findElement(By.id("blue")).click();
Step-by-Step Solution
Solution:
  1. Step 1: Identify unique selector for radio button

    Use CSS selector combining name and value attributes to target the correct radio button.
  2. Step 2: Evaluate options

    driver.findElement(By.cssSelector("input[name='color'][value='blue']")).click(); correctly uses CSS selector with name and value; driver.findElement(By.xpath("//input[@name='blue']")).click(); uses name='blue' instead of value='blue', incorrect.
  3. Final Answer:

    driver.findElement(By.cssSelector("input[name='color'][value='blue']")).click(); -> Option B
  4. Quick Check:

    Select radio by name and value using CSS selector [OK]
Quick Trick: Use CSS selector with name and value to select specific radio button [OK]
Common Mistakes:
MISTAKES
  • Using only name attribute which selects first radio button
  • Assuming id attribute exists for all radio buttons
  • Using XPath without specifying name attribute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes