【发布时间】:2019-02-04 10:57:39
【问题描述】:
使用 Java/Spring 标记我编写了一个 html 文件,其中包含一些从数据库获取的单选按钮。现在我正在编写 Selenium 测试用例以单击该单选按钮。当我使用 excel 表读取值时,我的单选按钮值在 Excel 表中。那么如何使用 excel 中的文本识别单选按钮并从 Selenium WebDriver 中单击它。
我已尝试使用 Xpath 并循环通过 WebElement 并使用 getText() 方法仍然得到空文本。
实际HTML:
<html>
<div class="col-xs-6 col-md-6 prop_value">
<c:forEach items="${products}" var="productTemp">
<form:radiobutton style="margin-left : 30px"
path="${mtool.productDetail.productId}" class="radiobtn"
name="product" value="${productTemp.productId}"/>${productTemp.productName}
</c:forEach></div>
</html>
现在当我在浏览器中检查这个 Html 时生成如下
已检查的 Html:
<div class="col-xs-8 col-md-8 prop_title">
Product<span class="required">*</span>
<input id="1" name="product" style="margin-left : 30px" class="radiobtn"
type="radio" value="1">CAR
<input id="2" name="product" style="margin-left : 30px" class="radiobtn"
type="radio" value="2">BUS
<input id="3" name="product" style="margin-left : 30px" class="radiobtn"
type="radio" value="3">TRUCK
<input id="4" name="product" style="margin-left : 30px" class="radiobtn"
type="radio" value="4">TRAIN
<input id="5" name="product" style="margin-left : 30px" class="radiobtn"
type="radio" value="5">AEROPLANE
</div>
这些值 CAR、BUS、TRUCK、TRAIN、AEROPLANE 存储在 Excel 表中。现在根据存储在 Excel 表中的值单击单选按钮,我编写 Selenium 代码如下。
Example.java
for(int i=0; i<totalRows; i++){
cell = sheet.getRow(i).getCell(0);
System.out.println(driver.findElement(By.xpath("//div[@class='col-xs-6 col-md-6 prop_value']")).getText());
List<WebElement> elmnt=driver.findElements(By.xpath("//input[@name='product' and @class='radiobtn']/following-sibling::text()/parent::div/input"));
for(WebElement list : elmnt){
String text=list.getText();
System.out.println("Text"+text);
if(text.equalsIgnoreCase(cell.getStringCellValue())){
list.click();
}
}
这只是与单选按钮相关的代码,所以我没有编写完整的类。现在我得到空文本,所以单选按钮没有被点击。所以有人可以让我知道如何做到这一点。谢谢你
【问题讨论】: