user1575134
user1575134

Reputation: 31

Issue selecting Radio button in Selenium Webdriver

<table id="Content_Content_Content_ctlCaseInfo_rdochldplcm" class="fltLeft">
        <tr>
            <td><input type="radio" id="Content_Content_Content_ctlCaseInfo_rdochldplcm_0" name="ctl00$ctl00$ctl00$Content$Content$Content$ctlCaseInfo$rdochldplcm" value="0" /><label for="Content_Content_Content_ctlCaseInfo_rdochldplcm_0">No</label></td><td><input type="radio" id="Content_Content_Content_ctlCaseInfo_rdochldplcm_1" name="ctl00$ctl00$ctl00$Content$Content$Content$ctlCaseInfo$rdochldplcm" value="1" /><label for="Content_Content_Content_ctlCaseInfo_rdochldplcm_1">Yes</label></td>
        </tr>
    </table>

When I try driver.FindElement(By.Id("Content_Content_Content_ctlCaseInfo_rdochldplcm")).Click(); it clicks to "Yes" When I try driver.FindElement(By.Id("Content_Content_Content_ctlCaseInfo_rdochldplcm_0")).Click(); OR driver.FindElement(By.Id("Content_Content_Content_ctlCaseInfo_rdochldplcm_1")).Click(); Nothing happens and no radio button gets selected.

Please suggest ways to handle this situation ..thanks a lot!!

Upvotes: 3

Views: 10499

Answers (5)

user3487861
user3487861

Reputation: 350

Try your code with the given below CSS :

Step 1: By Provided HTML Piece we can derive the CSS of the Radio Button

css=#Content_Content_Content_ctlCaseInfo_rdochldplcm input 

Step 2:

Click on the radio button using Web Driver Code

driver.findElement
   (By.cssSelector("#Content_Content_Content_ctlCaseInfo_rdochldplcm input"))
      .click();

Upvotes: 0

Prabu Ananthakrishnan
Prabu Ananthakrishnan

Reputation: 4249

Use this :

//First get the list of values from the radio button

List < WebElement > elements = driver.findElements(By.cssSelector("table[id='Content_Content_Content_ctlCaseInfo_rdochldplcm'] > td"));

WebElement value;

//use loop for searching the particular element

for(WebElement element : elements){

//Getting the value of the element
value = element.findElement(By.cssSelector("label")).getText();

//condition to click on the element
  if(value.trim().equals("No")){ //Here value is hard coded. You can take from excel sheet also

    // If condition satisfies, it will click on the element

    element.findElement(By.cssSelector("input").click();
}

}

This can be used as a common function also.

Upvotes: 1

user2902144
user2902144

Reputation: 11

this solved my problem perfeclty

I have a page with 18 radio buttons in 6 groups which represented "Yes" "No" and "No Answer" I was trying to get them by ID but it was randomized by the app But using a name and value tags made it work.

radios were defined basically like this:

input value="2" class=" x-form-radio x-form-field" autocomplete="off" id="randID_13578" name="emailNotifiyOptionAllow" type="radio">

and every time i opened this page id was different so using

"//input[contains(@name, 'emailNotifyOptionAllow') and contains(@value, 1)]"

solved it.

Thanx

Upvotes: 1

Michael Bautista
Michael Bautista

Reputation: 1220

It would probably be better to click the Radio buttons through XPath.
In your specific case, the XPath for:

Yes - Radio Button:

"//input[contains(@id, 'rdochldplcm') and contains(@value, 1)]"

No - Radio Button:

"//input[contains(@id, 'rdochldplcm') and contains(@value, 0)]"

In this instance, if you wanted to click the 'Yes' Radio button, you can do this:

string yesRadioButtonXPath = "//input[contains(@id, 'rdochldplcm') and contains(@value, 1)]"
IWebElement yesRadioButton = driver.FindElement(By.XPath(yesRadioButtonXPath));
yesRadioButton.Click();

For the 'No' Radio button, you would use this:

string noRadioButtonXPath = "//input[contains(@id, 'rdochldplcm') and contains(@value, 0)]"
IWebElement noRadioButton = driver.FindElement(By.XPath(noRadioButtonXPath));
yesRadioButton.Click();

Since you're using a table, there may be a chance that the XPath may return more than one element. You'd need to use a different method to sort out the elements in that case, but for what you're looking for, this method should work.

Upvotes: 3

tebel
tebel

Reputation: 23

try [0] and [1] instead of the underscore.

Upvotes: 0

Related Questions