首页 > 解决方案 > SELENIUM JAVA - 验证字符串的部分内容

问题描述

我想验证从网页获得的字符串的部分值。我举了一个例子,字符串是“Incident 1946721 Updated”,我想插入一个检查,验证这两个单词 Incident 作为前缀存在,Updated 作为后缀。我能怎么做?

文本所在的 html 代码是:

<div class="modal-body"><button type="button" class="bootbox-close-button close" data-dismiss="modal" aria-hidden="true" style="margin-top: -10px;">×</button><div class="bootbox-body">Incident 1946721 Updated</div></div>

标签: javaseleniumtestingautomation

解决方案


使用以下代码:

String value = driver.findElement(By.xpath("//div[@class='bootbox-body']")).getText();

if(value.startsWith("Incident") && value.endsWith("Updated")) {
    System.out.println("Test Pass");
}else {
    System.out.println("Test Fail");
}

如果您有进一步的疑问,请告诉我。


推荐阅读