java Selenium如何验证文本的值

时间:2018-09-06 10:03:33

标签: java html selenium automation

如何检查表中文本的值。我正在使用Java和硒类

我想验证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>

3 个答案:

答案 0 :(得分:2)

您已使用绝对xpath,这在自动化脚本中不建议使用。由于它包含HTML索引,并且如果DOM随结构更改,则该xpath将不再起作用。您应该使用相对xpath。

节点的相对xpath://a[@title='Title value of this attribute']//td

同样,您可以使用<a>标签的任何属性并找到<td>

您可以通过Java equal方法检查它,

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");
}

答案 1 :(得分:1)

  1. 在找到的元素上使用getText()方法以查找文本
  2. 使用java equals方法验证是否为预期文本
  3. 您还可以使用testNG断言来验证值。

      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");
    

答案 2 :(得分:0)

如果通过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());