-1

this is backend code for radio button

<div>
  <input type="radio" name="notification_pref" title="" value="1" 
  class="notifyRadio" checked=""><p>By Email</p>
</div>

<div>
  <input type="radio" name="notification_pref" title="" value="2" 
  class="notifyRadio"><p>By Text Message - Message and Data Rates May 
  Apply</p>
</div>

How can I handle this radio button?

  • What is a "back-end" code? Do you have some trial that failed? – Alexey R. Apr 22 at 13:04
  • <div><input type="radio" name="notification_pref" title="" value="1" class="notifyRadio" checked=""><p>By Email</p></div> <div><input type="radio" name="notification_pref" title="" value="2" class="notifyRadio"><p>By Text Message - Message and Data Rates May Apply</p></div> i have tried select method, by name, by xpath – Sufiyaan Apr 22 at 13:06
1

You work with radio buttons like you would work with a regular WebElement.

  1. Locate the element: WebElement myRadio = driver.findElement(By.xpath("//input[@type='radio'][@value='1']"));
  2. Perform a click: myRadio.click();

The code above locates your first radio-button and selects it.

UPD: There are a number of approaches that you can use to skip selection of already selected buttons (which one to use depends on your particular need). For example you can take all radio-buttons:

List<WebElement> allButtons = driver.findElements(By.xpath("//input[@type='radio']"));

and then iterate over the list to click first unselected button:

for(WebElement button: allButtons){
    if(!button.isSelected()){
        button.click();
        break;
    }
}
  • buddy, it didn't work. as value 1 is already checked but wants to check value 2. – Sufiyaan Apr 22 at 13:25
  • when u change to 2 does that work ? – Michael Durrant Apr 22 at 13:46
  • @Sufiyaan then you call isSelected to check if the element was already selected. and click if it was not. – Alexey R. Apr 22 at 13:53

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.