如何通过 WebDriver 验证加载页面中存在的文本 [英] How to verify a Text present in the loaded page through WebDriver

查看:19
本文介绍了如何通过 WebDriver 验证加载页面中存在的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 WebDriver 验证页面中存在的文本.我喜欢将结果视为布尔值(真或假).任何人都可以通过提供 WebDriver 代码来帮助解决这个问题吗?

I need to verify a Text present in the page through WebDriver. I like to see the result as boolean (true or false). Can any one help on this by giving the WebDriver code?

推荐答案

正如 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));

这篇关于如何通过 WebDriver 验证加载页面中存在的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆