Class By.ByClassName
The Class By.ByClassName is specifically defined in By.java. It enables finding elements based on the value of the "class" attribute, allowing matching against each individual class within an element.
/**
* Find elements based on the value of the "class" attribute. If an element has multiple classes, then
* this will match against each of them. For example, if the value is "one two onone", then the
* class names "one" and "two" will match.
*
* @param className The value of the "class" attribute to search for.
* @return A By which locates elements by the value of the "class" attribute.
*/
public static By className(String className) {
return new ByClassName(className);
}
Application
According to this definition, passing multiple classes like value
and test
as arguments to @FindBy(className = "...")
is not allowed. Attempting to do so will result in an error:
invalid selector: Compound class names not permitted
Resolution
To address this restriction, there are various strategies that can be employed:
If the element can be uniquely identified using only the classname
value
, you can utilize:
@FindBy(className = "value")
@CacheLookup
private WebElement test;
If the element is identifiable solely through the classname
test
, you can opt for:
@FindBy(className = "test")
@CacheLookup
private WebElement test;
In cases where both classnames
- value
and test
are essential for identification, CSS selectors can be utilized:
@FindBy(css = ".value.test")
@CacheLookup
private WebElement test;
Alternatively, XPath can also be employed:
@FindBy(xpath = "//*[@class='value test']")
@CacheLookup
private WebElement test;
Quick Reference
Solving the Invalid Selector Error Due to Compound Class Names Using Selenium