'isSelected() method for checkbox always returns false

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?



Solution 1:[1]

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

Solution 2:[2]

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!

Solution 3:[3]

       WebElement chck = driver.findElement(By.id("chkRemeberMe"));

        if(!chck.isSelected())
        {                 
                System.out.println(" CheckBox Not Selected");
        }
        else
        {
            System.out.println("CheckBox Already Selected");
        }

Solution 4:[4]

What I have found that if your checkbox has tagname='input' and type='checkbox', then only isSelected() will return a boolean value.

Otherwise, we have to look for another attribute that is getting changed on the check and uncheck.

Solution 5:[5]

If all else fails, try the is isDisplayed()

driver.findElement(By.xpath(xpath).isDisplayed()

Solution 6:[6]

In my view, isSelected() returns true only if it is a default selection. It may not work when we select an element and try to check if it is selected or not. Correct me if I am mistaken. Ankit

Solution 7:[7]

The problem might be with the xcode you are writing. Check if you can get a more unique xcode for the checkbox only something like

"/html/body/div[3]/div[2]/form/fieldset/div[1]/table/tbody/tr[10]/td/img[@type='checkbox']"

A checkbox xtype should mostly end with type=checkbox for isSelected to work

Solution 8:[8]

boolean select = driver.findElementByXPath("xpath").isSelected();
Assert.assertEquals(select, "true");

If is selected the checkbox then this case will not get fail.

Solution 9:[9]

Your findElement() call is missing .click to check or select the check box or radio

WebElement we  = findElement(By.xpath("some path));
we.click();
we.isSelected() => true

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 faizan sayyed
Solution 4
Solution 5 Mohammad Kanan
Solution 6 Ankit
Solution 7 SUPARNA SOMAN
Solution 8 Dave
Solution 9 bcar