Question

I'm using Selenium in C#, and I need to be able to specify which radio button on a website to click based on the text of the label that is associated with that radio button. I need to be able to pass that text in as a parameter. Here is a sample of what the code for the radio buttons looks like (this is for three buttons):

<input id="radSelect152_222_1369" class="bgRadioNotChecked" type="radio" onclick="BGQ.Ajax.SelectAnswer(this, '152','1369','3', '547');" value="1369" onfocus="BGQ.View.setLastElement(true);" name="radSelect152_222">
<label class="inline" for="radSelect152_222_1369">Watermelon</label>

<input id="radSelect152_222_1370" class="bgRadioNotChecked" type="radio" onclick="BGQ.Ajax.SelectAnswer(this, '152','1370','3', '547');" value="1370" onfocus="BGQ.View.setLastElement(true);" name="radSelect152_222">
<label class="inline" for="radSelect152_222_1370">Papaya</label>

<input id="radSelect152_222_1371" class="bgRadioNotChecked" type="radio" onclick="BGQ.Ajax.SelectAnswer(this, '152','1371','3', '547');" value="1371" onfocus="BGQ.View.setLastElement(true);" name="radSelect152_222">
<label class="inline" for="radSelect152_222_1371">Mango</label>

I want to be able to specify "Mango" in an Excel input file (I have all the file input stuff working fine) and have Selenium click the associated radio button. One approach that I read that I've been trying is to do the following:

  1. Find the element that has the target text ("Mango") as its text
  2. Get the FOR attribute from that element
  3. Find the input that has an id equal to that FOR attribute
  4. Click that input element.

Problem is, I'm pretty new to Selenium and can't quite figure the syntax for how to do these four steps. Can someone show me the way, with specific code examples? Also, or alternatively, is there a better/smarter way to do this? If so, please be specific.

I've included here the method I've started to write, in which I pass in the target text. I know it's wrong (especially in the By.XPath part).

    public void ClickRadioButtonByLabelText(string labelText)
    {
        // (1) Find the element that has the label text as its text
        IWebElement labelForButton = commondriver.FindElement(By.XPath(//label[text()='labelText']));
        // (2) Get the FOR attribute from that element
        string forAttribute 
        // (3) Find the input that has an id equal to that FOR attribute
        // (4) Click that input element
    }

Thanks.

Was it helpful?

Solution

You have two options.

Option 1 - do it all within a single XPath query

The XPath query would be:

//input[@id=//label[text()='TestCheckBox']/@for]

That is, get input that has an id which comes from the for attribute from a label that has the text of "TestCheckBox"

Option 2 - get the attribute from the label and then find the element in seperate steps

public void ClickRadioButtonByLabelText(string labelText)
{
    // (1) Find the element that has the label text as its text
    IWebElement labelForButton = commondriver.FindElement(By.XPath("//label[text()='labelText']"));

    // (2) Get the FOR attribute from that element
    string forAttribute = labelForButton.GetAttribute("for");

    // (3) Find the input that has an id equal to that FOR attribute
    IWebElement radioElement = commondriver.FindElement(By.Id(forAttribute));

    // (4) Click that input element
    radioElement.Click();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top