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?

+4


source to share


8 answers


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!

+4


source


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

+3


source


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

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

      

+1


source


.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

      

0


source


If isDisplayed()

, tryisDisplayed()

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

      

0


source


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

0


source


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

0


source


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.

0


source







All Articles