[英]isTextPresent fails all the time in Selenium test. Not sure if I am doing anything wrong
I have this little piece of code that just checks for a particular text at a location on the page and if that text is found, it will check for something else..Here is the snippet: 我有这小段代码,仅检查页面上某个位置的特定文本,如果找到了该文本,它将检查其他内容。这是代码段:
if (!selenium.isTextPresent("//div[contains(concat('',@class,''),'contentLg')]/h2")) { System.out.println("The About Us text is not present"); if(!selenium.isTextPresent(“ // div [contains(concat('',@ class,''),'contentLg')] / h2”)){System.out.println(“关于我们的文本不是当下”); } else { verifyEquals(aboutText, "//*[@class='content contentLg']/p[3]"); } else {verifyEquals(aboutText,“ // * [@ class ='content contentLg'] / p [3]”); } }
Here is the generated output that I am trying to grab the text "About Us" from. 这是生成的输出,我试图从中获取文本“关于我们”。 I am trying to see if the page has "About Us" text (which is in 我正在尝试查看页面上是否有“关于我们”文本(位于
<!-- MAIN CONTENT CONTAINER -->
About Us 关于我们
<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>
Here is what I wrote in a CustomSelenium class that extends DefaultSelenium. 这是我在扩展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;
}
Just call this before you call isTextPresent. 只需在调用isTextPresent之前调用它即可。 This is a Selenium bug . 这是一个硒错误 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.