Java 如何检查元素是否包含特定的类属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32713009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:58:28  来源:igfitidea点击:

How to check if element contains specific class attribute

javacssseleniumselenium-webdriverwebdriver

提问by aurelius

How can I check if a selenium web element contains a specific css class.

如何检查 selenium web 元素是否包含特定的 css 类。

I have this html li element

我有这个 html li 元素

<li class="list-group-item ng-scope active" ng-repeat="report in lineageController.reports" ng-click="lineageController.activate(report)" ng-class="{active : lineageController.active == report}">

As you can see inside class attribute there is an active class.

正如您在 class 属性中看到的那样,有一个活动类。

My problem is that I have this element and I want to do a check based on if the class attribute has that "active" value among the others, being more elegant solution then using xpath.

我的问题是我有这个元素,我想根据 class 属性是否具有“active”值进行检查,这是比使用 xpath 更优雅的解决方案。

How can I do this?

我怎样才能做到这一点?

采纳答案by drkthng

Given you already found your element AND you want to check for a certain class inside the class-attribute:

鉴于您已经找到了您的元素,并且您想检查 class-attribute 中的某个类:

public boolean hasClass(WebElement element) {
    String classes = element.getAttribute("class");
    for (String c : classes.split(" ")) {
        if (c.equals(theClassYouAreSearching)) {
            return true;
        }
    }

    return false;
}

EDIT

编辑

As @aurelius rightly pointed out, there is an even simpler way (that doesn't work very well):

正如@aurelius 正确指出的那样,还有一种更简单的方法(效果不佳):

public boolean elementHasClass(WebElement element, String active) {
    return element.getAttribute("class").contains(active);
}

This approach looks simpler but has one big caveat:

这种方法看起来更简单,但有一个很大的警告:

As pointed out by @JuanMendes you will run into problems if the class-name you're searching for is a substring of other class-names:

正如@JuanMendes 所指出的,如果您要搜索的类名是其他类名的子字符串,您将遇到问题:

for example class="test-a test-b", searching for class.contains("test") will return true but it should be false

例如 class="test-a test-b",搜索 class.contains("test") 将返回 true 但它应该是 false

EDIT 2

编辑 2

Try combining the two code snippets:

尝试组合两个代码片段:

public boolean elementHasClass(WebElement element, String active) {
    return element.getAttribute("class").split(" ").contains(active);
}

That should fix your caveat.

那应该可以解决您的警告。

回答by uesports135

The answer provided by @drkthng works but you might have a case where the class name is a subset of another class name. For example:

@drkthng 提供的答案有效,但您可能会遇到类名是另一个类名的子集的情况。例如:

<li class="list-group-item ng-scope active">text</li>

If you wanted to find the class "item" then the provided answer would give a false positive. You might want to try something like this:

如果您想找到“项目”类,那么提供的答案将给出误报。你可能想尝试这样的事情:

public boolean hasClass(WebElement element, String htmlClass) {
    String classes = element.getAttribute("class").split("\s+");
    if (classes != null) {
        for (String classAttr: classes) {
            if (classAttr.equals(htmlClass)) {
                return true;
            }
        }
    }
    return false;
}

回答by Ranet Cientouno

Try using contains();

尝试使用 contains();

String classes = divPubli.getAttribute("class");
assertTrue("The element does not contain .maClass class .publi",classes.contains("maClass"));

回答by Krystian

Simmilar to previous one, but with java 8 capabilities:

与上一个类似,但具有 java 8 功能:

String classes= getDriver().findElement(someSelector).getAttribute("class");
Optional<String> classFindResult = Arrays.stream(elementClasses.split(" ")).filter(el -> el.equals("myClass")).findFirst();
if(openClassFindResult.isPresent()){
    return true;
}
return false;

回答by JohnP2

Use javascript: classList.contains

使用 javascript:classList.contains

 WebElement element = By.id("id");
 String className = "hidden";
 JavascriptExecutor js = (JavascriptExecutor) driver;
 Boolean containsClass = js.executeScript("return arguments[0].classList.contains(arguments[1])", element, className);

回答by John Collins

Improving on @uesports135 answer, "classess" should be a String array.

改进@uesports135 答案,“classess”应该是一个字符串数组。

 public boolean hasClass(WebElement element, String htmlClass) {
        String[] classes = element.getAttribute("class").split("\s+");
        if (classes != null) {
            for (String classAttr: classes) {
                if (classAttr.equals(htmlClass)) {
                    return true;
                }
            }
        }
        return false;
    }