0

HTML code:

<div class="form-group">
    <label for="exampleInputEmail1">Gender</label>
    <input id="optionsRadios1" type="radio" value="Male" checked="" name="gender"/>
    Male
    <input id="optionsRadios1" type="radio" value="Female" name="gender"/>
    Female
<div>

How do I select and click on the radio button if it's already Checked?

  • Welcome to SQA! In the future please include actual code rather than a screenshot--this will make it easier for others to read and help you. Also, it looks like both of your radio buttons have the same ID, which is bound to cause problems. – c32hedge May 23 '17 at 16:45
1

First you need to check whether the radio button is selected or not.

Second if the radio button is already selected don't do anything but if the radio button is not selected then you need to click on the radio button.

You can do this using the below code.

//Getting the radio button webelement.
WebElement radio_Button = driver.findElement(By.id("xyz");

//Checking if the radio is selected or not
if(!radio_Button.isSelected())
{
  radio_Button.click();//If the radio button is not selected click on the radion button
}
0

There is already an answer here:

https://stackoverflow.com/questions/23800792/how-to-check-if-the-radio-button-is-selected-or-not-in-selenium-webdriver

Try checking if button is selected, then clicking

if (driver.findElement(By.Id(' ')).isSelected() == false)
  {
       driver.findElement(By.Id(' ')).Click()
  }
  • 3
    .isSelected() method returns a Boolean. So no need to compare it with false – Alok May 22 '17 at 12:16
0

Here is the answer in Python, in case you're not using Java.

radio_button = driver.find_element_by_id("<id>")

if radio_button.is_selected():
    radio_button.click()

More information can be found here: https://stackoverflow.com/questions/14442636/how-can-i-check-if-a-checkbox-is-checked-in-selenium-python-webdriver

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.