title: Using Selenium, how to click a radio button based on associated label
body: <p>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):</p>
  
  <pre><code>&lt;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"&gt;
  &lt;label class="inline" for="radSelect152_222_1369"&gt;Watermelon&lt;/label&gt;
  
  &lt;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"&gt;
  &lt;label class="inline" for="radSelect152_222_1370"&gt;Papaya&lt;/label&gt;
  
  &lt;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"&gt;
  &lt;label class="inline" for="radSelect152_222_1371"&gt;Mango&lt;/label&gt;
  </code></pre>
  
  <p>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:</p>
  
  <ol>
  <li>Find the element that has the target text ("Mango") as its text</li>
  <li>Get the FOR attribute from that element</li>
  <li>Find the input that has an id equal to that FOR attribute</li>
  <li>Click that input element.</li>
  </ol>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <pre><code>    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
      }
  </code></pre>
  
  <p>Thanks.</p>