让Selenium单击没有ID或名称的单选按钮

时间:2019-06-18 19:16:04

标签: c# selenium-webdriver

我正在尝试对页面进行Selenium测试,但无法单击单选按钮

我正在尝试使用IWebDriver xpath方法

<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>

这就是我正在使用的...

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

我希望它单击单选按钮,但出现此错误:

  OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(text(),'Text2')]"}

1 个答案:

答案 0 :(得分:0)

尝试使用此:

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

我认为正在发生的事情是您的页面上没有'button'元素,因此没有'NoSuchElementException'。

相关问题