如何使用以下方法单击单选按钮列表中的必需单选按钮

时间:2013-10-31 07:34:39

标签: java selenium-webdriver

<tr class="odd">
<td>
<input id="rightsTokenOid" type="radio" value="rightsTokenOid" onclick="setRightsTokenOid('urn:dece:rightstokenid:org:dece:E9807B96D4153534E040A8C0158E4EB8');" style="width:100%;border:0px" name="rightsTokenOid">
</td>
<td>urn:dece:rightstokenid:org:dece:E9807B96D4153534E040A8C0158E4EB8</td>
</tr>
<tr class="even">
<td>
<input id="rightsTokenOid" type="radio" value="rightsTokenOid" onclick="setRightsTokenOid('urn:dece:rightstokenid:org:dece:EA07E75226B329B1E040A8C0158E1813');" style="width:100%;border:0px" name="rightsTokenOid">
</td>
<td>urn:dece:rightstokenid:org:dece:EA07E75226B329B1E040A8C0158E1813</td>
</tr>

我使用以下方法点击单选按钮但它无法正常工作。请建议我使用以下方法点击所需的单选按钮

String RightsTokenID = urn:dece:rightstokenid:org:dece:EA07E75226B329B1E040A8C0158E1813;

List<WebElement> radios = driver.findElements(By.name("rightsTokenOid"));

for (WebElement element : radios) {

    if (element.getAttribute("value").equals(RightsTokenID)) {
       element.click();
    }
    break;
}

3 个答案:

答案 0 :(得分:0)

两个单选按钮的

nameidvalue相同。尝试给他们独特的价值观。

编辑: 尝试比较onclick

String RightsTokenID = "urn:dece:rightstokenid:org:dece:EA07E75226B329B1E040A8C0158E1813";

List<WebElement> radios = driver.findElements(By.name("rightsTokenOid"));

System.out.println("listsize: " + radios.size());
for (WebElement element : radios) {
    // just for debugging regarding this question
    System.out.println("tagname: " + element.getTagName());
    System.out.println("name: " + element.getAttribute("name"));
    System.out.println("type: " + element.getAttribute("type"));
    System.out.println("value: " + element.getAttribute("value"));
    System.out.println("onclick: " + element.getAttribute("onclick"));

    if (element.getAttribute("onclick").contains(RightsTokenID)) {
        element.click();
        break;
    }
}

答案 1 :(得分:0)

使用xpath

中的position()帮助

第一个单选按钮//input[@id='rightsTokenOid']and position()=1

的xpath

第二个单选按钮//input[@id='rightsTokenOid']and position()=2

的xpath

答案 2 :(得分:0)

这个怎么样?将onclick视为属性并查看它的值是否具有正确的令牌?

String RightsTokenID = urn:dece:rightstokenid:org:dece:EA07E75226B329B1E040A8C0158E1813;

List<WebElement> radios = driver.findElements(By.name("rightsTokenOid"));

for (WebElement element : radios) {

    if (element.getAttribute("onclick").contains(RightsTokenID)) {
       element.click();
    }
break;
}