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

查看:87
本文介绍了如何通过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天全站免登陆