我正在使用Coded UI为Web应用程序创建一些测试用例,同时我也遇到了一个问题。我无法使用显示的文本选择单选按钮,但是如果我使用ValueAttribute则其工作正常。但是,由于value属性不包含对于创建测试数据的人来说可能没有任何逻辑用途的数字,因此我需要使用“单选按钮的显示文本”执行相同的工作。
这是我的HTML代码
<td><input id="ContentPlaceHolder1_rbl_NewChanged_0" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1131">
<label for="ContentPlaceHolder1_rbl_NewChanged_0">New</label></td>
<td><input id="ContentPlaceHolder1_rbl_NewChanged_1" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1132">
<label for="ContentPlaceHolder1_rbl_NewChanged_1">Changed</label></td>
<td><input id="ContentPlaceHolder1_rbl_NewChanged_2" type="radio" name="ctl00$ContentPlaceHolder1$rbl_NewChanged" value="1133">
<label for="ContentPlaceHolder1_rbl_NewChanged_2">Longstanding</label></td>
我尝试过以下代码。但没有工作
String selectType = data.getType().get(rowCnt);// data reading from excel stored to string variable
List<WebElement> type = driver.findElements(By.xpath("//input[@type='radio']"));
for (int i = 0; i < type.size(); i++) {
if (type.get(i).getText().equals(selectType)) {
type.get(i).click();
}
}
答案 0 :(得分:0)
如果您获得与String
标签文字相关的<label>
值,例如新的,改变的,长期的。等等,你可以编写一个函数,将文本视为String
,如下所示:
public void clickItem(String itemText)
{
driver.findElement(By.xpath("//td//label[starts-with(@for,'ContentPlaceHolder1_rbl_NewChanged_')][.='" + itemText + "']")).click();
}
现在你可以通过传递clickItem()
参数来点击相关的单选按钮来调用函数String
,如下所示:
String selectType = data.getType().get(rowCnt); // data reading from excel stored to string variable
clickItem(selectType); // will support clickItem("New"), clickItem("Changed") and clickItem("Longstanding")
注意:根据您提供的 HTML ,clickItem()
方法应该与已实施的Locator Strategy一起使用。作为替代方案,您还可以替换clickItem()
函数以单击<input>
节点,如下所示:
public void clickItem(String itemText)
{
driver.findElement(By.xpath("//td//label[.='" + itemText + "']//preceding::input[1]")).click();
}