-1

我正在使用 apache commons 库中的 StringUtils 来检查 html 页面源代码中子字符串的匹配数。

我已经使用 webdriver 命令转换了页面源:

String pageSource = driver.getPageSource();    

我用来查找匹配项的代码是:

int cloudfCount = StringUtils.countMatches(pageSource, "cloudfront");
System.out.println("There are " + cloudfCount + " instances of cloudf text found within the page source.");

当我手动执行检查时,我得到 2 个匹配项。这是我所期待的。

但是当我在自动化的 Selenium 脚本中使用上述代码时,我得到了 5 个结果。

关于我哪里出错的任何想法/想法?

谢谢你。

4

1 回答 1

0

@kevin 如果您可以向我们提供页面源的内容,那就太好了。另一件事,如果页面已被修改,则无需获取当前页面源,请参考getPageSource Doc

如果无论如何 getPageSource 不起作用并且您需要正文标记中的文本,您可以使用: document.getElementsByTagName('body')[0].textContent使用 JavascriptExecutor,

然后试一试StringUtils.countMatches()

谢谢。

于 2014-11-26T06:01:04.280 回答