There is a check box which is displaying as checked already, now when I inspect it shows with image src. in HTML. When I click on the checkbox, it is getting unchecked or checked.
To verify its state, I have written this code, which always brings false even though the checkbox is selected.
WebElement chBox = driver.findElement(By.xpath
("/html/body/div[3]/div[2]/form/fieldset/div[1]/table/tbody/tr[10]/td/img"));
if (chBox.isSelected()) {
System.out.println("User active check box is already checked");
} else
System.out.println("User active check box is not checked");
}
Why?
Check the class attribute is getting changed on Check/ Uncheck the check box. If so, then the selection state is stored as part of the class
String Class=chk.getAttribute("class");
if(Class.contains("class name when it is checked"))
{
System.out.println("Status: "+chk.getAttribute("checked"));
//This will return Null, since it is not a real check box(type=checkbox),
//so there is no checked attribute in it
}
else
{
System.out.println("Not Checked");
}
The isSelected()
method will not handle this type of checkbox, that's why it always returns false
or not checked(from your point of view)
Refer: here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With