我有一小段代码只检查页面上某个位置的特定文本,如果找到该文本,它会检查其他内容。这是代码片段:
if(!selenium.isTextPresent(“// div [contains(concat('',@ class,''),'contentLg')] / h2”)){ System.out.println(“关于我们的文本不存在”); } 其他{ verifyEquals(aboutText,“// * [@ class ='content contentLg'] / p [3]”); }
这是生成的输出,我试图从中获取文本“关于我们”。我试图查看该页面是否有“关于我们”文本(位于
<!-- MAIN CONTENT CONTAINER -->
关于我们
<p><span class="faqQuestion">About The Site</span><br>
this is about the site</p>
<p>test one two three</p>
<p>this test test test test test test</p>
<br>
<p><span class="faqQuestion">About Company</span><br>
This is about the company</p>
<p>fafasdfsafasdfafasfasfasasfasfasfafsasf</p>
<p>afasfasdfasdffafffffffffffffffffffffffffffffffffffffffffffffffffffffffffff</p>
<br>
<p><span class="faqQuestion">Helping Businesses</span><br>
Helping business
<a href="/businesses.asp">click here</a> to learn more.</p>
答案 0 :(得分:0)
以下是我在扩展DefaultSelenium的CustomSelenium类中编写的内容。
public boolean waitForTextPresent(final String text, long timeout) {
Wait wait = new Wait() {
@Override
public boolean until() {
try {
return CustomSelenium.this.isTextPresent(text);
} catch (SeleniumException e) {
return false;
}
}
};
try {
wait.wait("Error: text " + text + " not present in the page", timeout, 50);
} catch (NumberFormatException e) {
log.error(e.getMessage(), e);
throw e;
} catch (WaitTimedOutException e) {
log.warn(e.getMessage(), e);
return false;
}
return true;
}
在调用isTextPresent之前调用它。这是a Selenium bug。