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?
WebElement chBox = driver.findElement(By.id("chkIsActive"));
if (chBox.isSelected())
{
System.out.println("User active check box is already checked");
}
else
{
System.out.println("User active check box is not checked");
}
Hope this helps!
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
WebElement chck = driver.findElement(By.id("chkRemeberMe"));
if(!chck.isSelected())
{
System.out.println(" CheckBox Not Selected");
}
else
{
System.out.println("CheckBox Already Selected");
}
.Click is missing in your call to findElement () to check or set checkbox or radio
WebElement we = findElement(By.xpath("some path));
we.click();
we.isSelected() => true
If isDisplayed()
, tryisDisplayed()
driver.findElement(By.xpath(xpath).isDisplayed()
In my opinion, isSelected () only returns true if it is the default selection. It may not work when we select an item and try to check if it is selected or not. Correct me if I am wrong. Ankit
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
I found that if your checkbox has tagname='input'
and type='checkbox'
, then it isSelected()
will only return a boolean value.
Otherwise, we have to look for another attribute that changes on the check and uncheck the box.