0

I'm trying to set up a Selenium test of a page and can't get it to click on radio buttons

I'm trying to use the IWebDriver xpath method

<fieldset class="radio-button-list">
                            <label >
                                <span class="className">Text1</span>
                                <input type="radio" value="Button1">
                            </label>
                            <label>
                                <span class="className">Text2</span>
                                <input type="radio" value="Button2">
                            </label>
</fieldset>

This is what I'm using...

driver.FindElement(By.XPath("//button[contains(text(),'Text2')]")).Click();

I expect it to click on the radio button but I'm getting this error:

  OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(text(),'Text2')]"}
orde
  • 5,233
  • 6
  • 31
  • 33
John
  • 371
  • 2
  • 4
  • 16
  • Are the fields always in the same order where you could do something like /html/body/fieldset/label[1]/input – Ryan Schlueter Jun 18 '19 at 19:21
  • They are. Could you clarify that code? Does that go inside the xpath? – John Jun 18 '19 at 19:23
  • yep thats the xpath of me just throwing it in a blank. html document. If it doesn't have a value I just go into chrome tools select the node and copy xpath. https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome – Ryan Schlueter Jun 18 '19 at 19:29
  • 2
    Can you not add an id? – Chris Jun 18 '19 at 21:18

1 Answers1

0

Try using this:

driver.FindElement(By.XPath(".//fieldset[@class='radio-button-list']/label[2]/input[@value='Button2']")).Click();

I think what's happening is there is no 'button' element on your page, hence the 'NoSuchElementException'.

Jason
  • 284
  • 2
  • 10