Selenium 2.0 Web驱动程序:isTextPresent的实现 [英] Selenium 2.0 Web Driver: implementation of isTextPresent

查看:82
本文介绍了Selenium 2.0 Web驱动程序:isTextPresent的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个可行的实现方法.我想出的最好的方法是:

I'm looking for a working implementation of this. The best I've come up with is:

    public boolean isTextPresent(String string) {

        for (WebElement e : drv.findElements(By.cssSelector("*"))) {

            if (e.isDisplayed() && e.getText().contains(string)) {
                return true;
            }

        }
        return false;
    }

推荐答案

一种更快的方法是这样的:

A faster way to do it would be something like this:

public boolean isTextPresent(string str)
{
    IWebElement bodyElement = driver.FindElement(By.TagName("body"));
    return bodyElement.Text.contains(str);
}

它在C#中,但概念相同.获取body标签的文本会自动返回所有嵌套元素的文本.我唯一不确定的是是否包含隐藏元素.

It's in C# but it's the same concept. Getting the text of the body tag automatically returns the text of all the nested elements. The only thing I'm not sure about is if hidden elements are included or not.

这篇关于Selenium 2.0 Web驱动程序:isTextPresent的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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