Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 412
user2092132 Avatar asked Aug 20 '14 03:08

user2092132


1 Answers

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

like image 86
Vignesh Paramasivam Avatar answered Oct 01 '22 12:10

Vignesh Paramasivam