-2
<br>
<input id="workedWithGR" type="radio" onclick="showDiv('hiddenInput');" value="yes" checked="" name="workedWithGR" style="border:none;">

<label>Yes</label>

<input id="workedWithGR" type="radio" onclick="hideDiv('hiddenInput');" value="no" name="workedWithGR" style="border:0px;">

对于是和否按钮,我不断收到“Webdriver 找不到元素错误”。我不能做 xpath,因为 id 中有引号。

4

2 回答 2

2

改用标签“值”或子节点“标签”,然后递归地向上树选择正确的元素。例如,我假设您的 html 具有以下内容:

<br>
  <input id="workedWithGR" type="radio" onclick="showDiv('hiddenInput');" value="yes" checked="" name="workedWithGR" style="border:none;">
    <label>Yes</label>
  <input id="workedWithGR" type="radio" onclick="hideDiv('hiddenInput');" value="no" name="workedWithGR" style="border:0px;">
    <label>No</label>

所以尝试像下面这样关闭节点标签:

//input[@id=\"workedWithGR\"]/label[text()="Yes"]/../input
于 2013-03-19T21:59:36.987 回答
0

I use this approach for radio input. Avoid having to look for multiple input elements / works with variable number of radio buttons having same name.

String valueToSelect = "no";
List<WebElement> radios = driver.findElement(By.name("workedWithGR"));
if (radios.size() == null)
    return ;

for (WebElement radio : radios){
    if (radio.getAttribute("value").equals(valueToSelect)){
        radio.click();
        break;
    }
}
于 2013-03-20T11:36:11.600 回答