Bird
Raised Fist0
Selenium Javatesting~20 mins

Radio button handling in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Radio Button Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that tries to select a radio button and print its status. What will be printed?
Selenium Java
WebElement radioBtn = driver.findElement(By.id("option1"));
radioBtn.click();
System.out.println(radioBtn.isSelected());
AElementNotInteractableException
Bfalse
CNoSuchElementException
Dtrue
Attempts:
2 left
💡 Hint
Clicking a radio button sets it as selected, so isSelected() returns true.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies a radio button is selected in Selenium Java?
You want to assert that a radio button with id 'genderMale' is selected. Which assertion is correct?
AassertEquals(driver.findElement(By.id("genderMale")).getText(), "selected");
BassertFalse(driver.findElement(By.id("genderMale")).isDisplayed());
CassertTrue(driver.findElement(By.id("genderMale")).isSelected());
DassertNull(driver.findElement(By.id("genderMale")).getAttribute("checked"));
Attempts:
2 left
💡 Hint
Use isSelected() to check if a radio button is selected.
❓ locator
advanced
2:00remaining
Which locator is best to select a radio button with label text 'Subscribe'?
You want to select the radio button associated with the label text 'Subscribe'. Which locator is best?
ABy.xpath("//label[text()='Subscribe']/preceding-sibling::input[@type='radio']")
BBy.id("Subscribe")
CBy.cssSelector("input[type='radio'][value='Subscribe']")
DBy.name("Subscribe")
Attempts:
2 left
💡 Hint
Use XPath to find input radio before label with exact text.
🔧 Debug
advanced
2:00remaining
Why does this Selenium Java code fail to select the radio button?
Given this code snippet, why does the radio button not get selected? WebElement radio = driver.findElement(By.name("choice")); radiobutton.click();
AThe click() method requires JavaScript executor for radio buttons.
BVariable name 'radiobutton' is undefined; should be 'radio'.
CRadio buttons cannot be clicked using Selenium WebDriver.
DThe locator By.name("choice") is incorrect for radio buttons.
Attempts:
2 left
💡 Hint
Check variable names carefully for typos.
❓ framework
expert
3:00remaining
In a Selenium Java test framework, which method best ensures a radio button is selected before proceeding?
You want to create a reusable method that selects a radio button only if it is not already selected. Which method implementation is best?
A
public void selectRadioButton(WebElement radio) {
  if (!radio.isSelected()) {
    radio.click();
  }
}
B
public void selectRadioButton(WebElement radio) {
  radio.click();
}
C
public void selectRadioButton(WebElement radio) {
  if (radio.isDisplayed()) {
    radio.click();
  }
}
D
public void selectRadioButton(WebElement radio) {
  if (radio.getAttribute("checked") == null) {
    radio.click();
  }
}
Attempts:
2 left
💡 Hint
Check selection status before clicking to avoid unnecessary actions.

Practice

(1/5)
1. What does the isSelected() method do when used on a radio button element in Selenium?
easy
A. Checks if the radio button is currently selected
B. Clicks the radio button to select it
C. Returns the text label of the radio button
D. Finds the radio button element on the page

Solution

  1. Step 1: Understand the purpose of isSelected()

    This method checks the current state of a radio button or checkbox to see if it is selected or not.
  2. Step 2: Differentiate from other methods

    click() selects the radio button, but isSelected() only checks selection status without changing it.
  3. Final Answer:

    Checks if the radio button is currently selected -> Option A
  4. Quick Check:

    isSelected() = Check selection status [OK]
Hint: Remember: isSelected() checks state, click() changes state [OK]
Common Mistakes:
  • Confusing isSelected() with click()
  • Expecting isSelected() to select the button
  • Using getText() to check selection
2. Which of the following is the correct way to select a radio button using Selenium WebDriver in Java?
easy
A. driver.findElement(By.id("option1")).isSelected();
B. driver.findElement(By.id("option1")).sendKeys("select");
C. driver.findElement(By.id("option1")).getText();
D. driver.findElement(By.id("option1")).click();

Solution

  1. Step 1: Identify the method to select a radio button

    To select a radio button, you use the click() method on the WebElement.
  2. Step 2: Check other options for correctness

    isSelected() only checks selection, getText() returns label text, and sendKeys() is not used for radio buttons.
  3. Final Answer:

    driver.findElement(By.id("option1")).click(); -> Option D
  4. Quick Check:

    Use click() to select radio button [OK]
Hint: Use click() to select radio buttons, not isSelected() [OK]
Common Mistakes:
  • Using isSelected() to select instead of check
  • Trying to sendKeys() to radio buttons
  • Using getText() expecting to select
3. Consider the following code snippet:
WebElement rb1 = driver.findElement(By.id("radio1"));
WebElement rb2 = driver.findElement(By.id("radio2"));
rb1.click();
boolean selected1 = rb1.isSelected();
boolean selected2 = rb2.isSelected();
What will be the values of selected1 and selected2 after execution?
medium
A. selected1 = true, selected2 = false
B. selected1 = false, selected2 = true
C. selected1 = true, selected2 = true
D. selected1 = false, selected2 = false

Solution

  1. Step 1: Understand radio button behavior on click

    Clicking rb1 selects it and deselects other radio buttons in the same group, so rb1.isSelected() returns true and rb2.isSelected() returns false.
  2. Step 2: Confirm the boolean values

    Therefore, selected1 is true and selected2 is false after clicking rb1.
  3. Final Answer:

    selected1 = true, selected2 = false -> Option A
  4. Quick Check:

    Click selects one, others deselect = true/false [OK]
Hint: Only one radio button in group is selected at a time [OK]
Common Mistakes:
  • Assuming both radio buttons can be selected simultaneously
  • Confusing isSelected() return values
  • Not understanding radio button group behavior
4. You have the following code to select a radio button:
WebElement radio = driver.findElement(By.name("gender"));
radio.click();

But the test fails because the wrong radio button is selected. What is the likely problem?
medium
A. The findElement method is incorrect; should use findElements
B. The click() method does not work on radio buttons
C. Using By.name returns multiple elements; need to specify which one
D. Radio buttons cannot be selected by Selenium

Solution

  1. Step 1: Analyze locator usage

    Using By.name("gender") returns the first matching element, but if multiple radio buttons share the same name, this may select the wrong one.
  2. Step 2: Fix by specifying correct element

    To select the intended radio button, use a more specific locator or find all elements and choose the correct one.
  3. Final Answer:

    Using By.name returns multiple elements; need to specify which one -> Option C
  4. Quick Check:

    Locator must uniquely identify radio button [OK]
Hint: Use unique locators or filter elements to select correct radio button [OK]
Common Mistakes:
  • Assuming findElement returns all matching elements
  • Thinking click() doesn't work on radio buttons
  • Ignoring that multiple radios share the same name attribute
5. You want to write a test to verify that selecting one radio button deselects the others in the same group. Which approach is best to implement this in Selenium Java?
hard
A. Click all radio buttons one by one and assert they are all selected
B. Click one radio button, then assert it is selected and all others are not selected
C. Use sendKeys() to select radio buttons and check their text
D. Check only the first radio button's selection status without clicking

Solution

  1. Step 1: Understand radio button group behavior

    Only one radio button in a group can be selected at a time; selecting one deselects others.
  2. Step 2: Write assertions to verify this

    Click one radio button, then check that it is selected and all others are not selected to confirm correct behavior.
  3. Final Answer:

    Click one radio button, then assert it is selected and all others are not selected -> Option B
  4. Quick Check:

    Test selection exclusivity by asserting one selected, others not [OK]
Hint: Assert one selected and others deselected after click [OK]
Common Mistakes:
  • Expecting multiple radio buttons to be selected simultaneously
  • Not asserting other buttons are deselected
  • Using sendKeys() instead of click()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard