问题在Selenium Webdriver中选择单选按钮

时间:2012-08-03 20:47:57

标签: selenium

<table id="Content_Content_Content_ctlCaseInfo_rdochldplcm" class="fltLeft">
        <tr>
            <td><input type="radio" id="Content_Content_Content_ctlCaseInfo_rdochldplcm_0" name="ctl00$ctl00$ctl00$Content$Content$Content$ctlCaseInfo$rdochldplcm" value="0" /><label for="Content_Content_Content_ctlCaseInfo_rdochldplcm_0">No</label></td><td><input type="radio" id="Content_Content_Content_ctlCaseInfo_rdochldplcm_1" name="ctl00$ctl00$ctl00$Content$Content$Content$ctlCaseInfo$rdochldplcm" value="1" /><label for="Content_Content_Content_ctlCaseInfo_rdochldplcm_1">Yes</label></td>
        </tr>
    </table>

当我尝试 。driver.FindElement(By.Id( “Content_Content_Content_ctlCaseInfo_rdochldplcm”))点击(); 它点击“是” 当我尝试driver.FindElement(By.Id(“Content_Content_Content_ctlCaseInfo_rdochldplcm_0”))。Click(); 要么 。driver.FindElement(By.Id( “Content_Content_Content_ctlCaseInfo_rdochldplcm_1”))点击(); 什么都没发生,没有选择单选按钮。

请建议处理这种情况的方法..非常感谢!!

5 个答案:

答案 0 :(得分:3)

通过XPath单击单选按钮可能会更好 在您的特定情况下,XPath:

是 - 单选按钮:

"//input[contains(@id, 'rdochldplcm') and contains(@value, 1)]"

不 - 单选按钮:

"//input[contains(@id, 'rdochldplcm') and contains(@value, 0)]"

在这种情况下,如果您想单击“是”单选按钮,则可以执行以下操作:

string yesRadioButtonXPath = "//input[contains(@id, 'rdochldplcm') and contains(@value, 1)]"
IWebElement yesRadioButton = driver.FindElement(By.XPath(yesRadioButtonXPath));
yesRadioButton.Click();

对于“否”单选按钮,您可以使用:

string noRadioButtonXPath = "//input[contains(@id, 'rdochldplcm') and contains(@value, 0)]"
IWebElement noRadioButton = driver.FindElement(By.XPath(noRadioButtonXPath));
yesRadioButton.Click();

由于您正在使用表,因此XPath可能会返回多个元素。在这种情况下,您需要使用不同的方法来排序元素,但是对于您要查找的内容,此方法应该可以正常工作。

答案 1 :(得分:1)

这解决了我的问题perfeclty

我有一个包含6个组中的18个单选按钮的页面,表示“是”“否”和“无应答” 我试图通过ID获取它们,但它是由应用随机化的 但是使用名称和值标签使其有效。

无线电的定义基本上是这样的:

  

输入值=“2”class =“x-form-radio x-form-field”autocomplete =“off”id =“randID_13578”name =“emailNotifiyOptionAllow”type =“radio”&gt;

每次打开此页面时,ID都不同,所以使用

  

“//输入[contains(@ name,'e​​mailNotifyOptionAllow')并包含(@ value,1)]”

解决了它。

感谢名单

答案 2 :(得分:1)

使用此:

//First get the list of values from the radio button

List < WebElement > elements = driver.findElements(By.cssSelector("table[id='Content_Content_Content_ctlCaseInfo_rdochldplcm'] > td"));

WebElement value;

//use loop for searching the particular element

for(WebElement element : elements){

//Getting the value of the element
value = element.findElement(By.cssSelector("label")).getText();

//condition to click on the element
  if(value.trim().equals("No")){ //Here value is hard coded. You can take from excel sheet also

    // If condition satisfies, it will click on the element

    element.findElement(By.cssSelector("input").click();
}

}

这也可以作为一种常用功能。

答案 3 :(得分:0)

尝试[0]和[1]而不是下划线。

答案 4 :(得分:0)

尝试使用以下CSS

下面给出的代码

第1步: 通过提供HTML片段,我们可以派生单选按钮的CSS

css=#Content_Content_Content_ctlCaseInfo_rdochldplcm input 

第2步:

使用网络驱动程序代码

单击单选按钮
driver.findElement
   (By.cssSelector("#Content_Content_Content_ctlCaseInfo_rdochldplcm input"))
      .click();
相关问题