且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过 WebDriver 验证加载页面中存在的文本

更新时间:2023-02-23 17:52:44

正如 zmorris 指出的那样 driver.getPageSource().contains("input"); 不是正确的解决方案,因为它在所有html,不仅仅是上面的文本.我建议检查这个问题:如何检查页面中是否存在某些文本?以及 Slanec 解释的推荐方式:

As zmorris points driver.getPageSource().contains("input"); is not the proper solution because it searches in all the html, not only the texts on it. I suggest to check this question: how can I check if some text exist or not in the page? and the recomended way explained by Slanec:

String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue("Text not found!", bodyText.contains(text));