-2

I am using Coded UI for creating some test cases for a web application, while doing the same I have encountered an issue. I am not able to select a Radio Button using their Displayed Text, however if I use the ValueAttribute then its working fine. But, since value attribute is not containing a number which may not be of any logical use for a person creating test data, so I need to do same work using the Displayed Text of the Radio button.

Here is my html code

<td><input id="ContentPlaceHolder1_rbl_NewChanged_0" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1131">
    <label for="ContentPlaceHolder1_rbl_NewChanged_0">New</label></td>
    <td><input id="ContentPlaceHolder1_rbl_NewChanged_1" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1132">
    <label for="ContentPlaceHolder1_rbl_NewChanged_1">Changed</label></td>
    <td><input id="ContentPlaceHolder1_rbl_NewChanged_2" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1133">
    <label for="ContentPlaceHolder1_rbl_NewChanged_2">Longstanding</label></td>

I have tried the following code. but didn't work

 String selectType = data.getType().get(rowCnt);// data reading from excel stored to string variable
                List<WebElement> type = driver.findElements(By.xpath("//input[@type='radio']"));
                for (int i = 0; i < type.size(); i++) {
                    if (type.get(i).getText().equals(selectType)) {
                        type.get(i).click();
                    }
                }
  • 1
    Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. – GPT14 May 31 '18 at 06:42
  • String selectType = data.getType().get(rowCnt);//data reading from excel stored to string variable System.out.println("radio buttons" +data.getType().get(rowCnt)); List type = driver.findElements(By.xpath("//input[@type='radio']")); System.out.println("type" +type); for (int i = 0; i < type.size(); i++) { if (type.get(i).getText().equals(selectType)) { type.get(i).click(); } } – Sreejitha J May 31 '18 at 06:47
  • Update the question with the code. – GPT14 May 31 '18 at 06:48
  • @SreejithaJ Are you getting the `String` values e.g. _New_, _Changed_, _Longstanding_. etc from excel? – DebanjanB May 31 '18 at 08:20
  • I am able to read data from excel , but cannot select a radio button using their displayed text, however if I use the ValueAttribute then its working fine. – Sreejitha J May 31 '18 at 08:40
  • To get the label of the radio button , you can use like this List type = driver.findElements(By.xpath("//input[@type='radio']/../label")); – Sijin May 31 '18 at 09:41

1 Answers1

0

If you are getting the String values pertaining to the text of the <label> tags e.g. New, Changed, Longstanding. etc from excel then you can write a function which will acccept the texts as String as follows:

public void clickItem(String itemText)
{
    driver.findElement(By.xpath("//td//label[starts-with(@for,'ContentPlaceHolder1_rbl_NewChanged_')][.='" + itemText + "']")).click();
}

Now you can call the function clickItem() by passing the String argument to click on the relevant Radio Button as follows:

String selectType = data.getType().get(rowCnt); // data reading from excel stored to string variable
clickItem(selectType); // will support clickItem("New"), clickItem("Changed") and clickItem("Longstanding")

Note: As per the HTML you have provided the clickItem() method should work with the implemented Locator Strategy. As an alternative you can also replace the clickItem() function to click on the <input> node as follows:

public void clickItem(String itemText)
{
    driver.findElement(By.xpath("//td//label[.='" + itemText + "']//preceding::input[1]")).click();
}
DebanjanB
  • 118,661
  • 30
  • 168
  • 217