The isSelected () method for a checkbox always returns false
There is a checkbox that shows as already checked, now when I check it, it shows up in the image src. in HTML. When I click on this checkbox, it is not checked or not checked.
To check its status, I wrote this code that always returns false even if this checkbox is checked.
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?
source to share
Check that the class attribute is changed in the Check / Uncheck box. If so, then the selection state is saved 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 method isSelected()
will not handle this type of checkbox, so it always returns false
or not checked (from your point of view) See here
source to share
Perhaps the problem is with the code you are writing. Check if you can get a more unique checkbox code just something like
"/ Html / body / div [3] / div [2] / form / FIELDSET / div [1] / table / TBODY / tr [10] / td / img [@type = 'checkbox']"
The xtype checkbox must basically end with type = checkbox for isSelected to work
source to share