如何检查表中文本的值。我正在使用java和selenium类
我想验证td中存在的值是“timetoresponse”
driver.findElement(By.xpath("//*[@id=\"IncidentSearch\"]/tbody/tr/td[33]"));
在HTML代码下面
<a tabindex="0" role="button" data-toggle="popover" data-trigger="focus" data-placement="auto" data-content="" data-original-title="" title=""></a><td style="">timeToResponse</td>
WebElement element =
driver.findElement(By.xpath("//*[@id='IncidentSearch']/tbody/tr/td[33]"));
if (element.getText().equals("timeToResponse")))
System.out.println("Match found");
else
System.out.println("Match Not found");
Assert.assertEquals(element.getText(), "timeToResponse");
您使用了绝对xpath,自动化脚本中不建议这样做。因为它包含HTML索引,并且如果DOM随结构改变,则此xpath不再起作用。您应该使用相对xpath。
节点的相对xpath://a[@title='Title value of this attribute']//td
同样明智的,你可以使用<a>
标签的任何属性并找到<td>
您可以通过Java等方法检查它,
String retrieveText = driver.findElement(By.xpath("//a[@title='Title value of this attribute']//td")).getText();
if(retrieveText.equals("timeToResponse")) {
//User defined message on console
System.out.println("Value match as Expected");
}
如果通过JUnit执行此测试,那么您可以使用:
void org.junit.Assert.assertEquals(String message, Object expected, Object actual)
即
assertEquals("timeToResponse Check ....","timeToResponse",driver.findElement(By.xpath("//a[@title='Title value of this attribute']//td")).getText());