How can I check if a selenium web element contains a specific css class.
I have this html li element
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:
- text
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;
}