When using a CSS Selector to click on the next sibling element in Selenium WebDriver, an InvalidSelectorException is thrown.
If we consider my DOM structure:
<div class="checkbox-group">
<div>
<span class="checkbox">::after</span> <!--clicking on this span checks the checkbox-->
<span class="checkbox-name">Male</span> <!--clicking on this span does not check the checkbox-->
</div>
<div>
<span class="checkbox">::after</span>
<span class="checkbox-name">Female</span>
</div>
</div>
Here is the Java code snippet:
@FindAll(@FindBy(css=".checkbox-name"))
List<WebElement> checkboxes;
public void selectCheckbox(String value){
for(WebElement checkbox : checkboxes){
String text = checkbox.getText();
if(text.equalsIgnoreCase(value)){
WebElement control = checkbox.findElement(By.cssSelector("+.checkbox"));//The exception occurs here
control.click();
}
}
}
The Exception message states:
org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified.
** Element info: {Using=css selector, value=+.checkbox}