isSelected() method for checkbox always returns false
Asked Answered
A

11

6

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?

Alda answered 20/8, 2014 at 3:30 Comment(8)
Please post html of the elementAnabal
Html of element isnt need the java is wrongGumbotil
@mfsi_sitamj Below are the html: <img src="/ABC30/Content/Images/TriStateCheckbox/checked.gif">Alda
Nevermind i see you're gathering the element and prechecking it. It should always return false is what you're getting at but its returning true?Gumbotil
@Gumbotil exactly what its reason?Alda
Thanks for putting the html markup. Can you see whether there is a change in the html before and after selecting the check-box. If yes, can you please the corresponding mark ups (Before selecting and after selecting) here?Anabal
Pardon! There was a typo in the above line : If yes, can you please post the corresponding mark ups (Before selecting and after selecting) here?Anabal
@mfsi_sitamj in the html it change the code inplace of "Checked.gif" or "Unchecked.gif".Alda
K
4

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

Karlkarla answered 20/8, 2014 at 9:43 Comment(4)
Below are the html of that checkbox and in these its not displaying any classes at tht time of inspecting a check box: <td align="left" colspan="2"> <input id="chkIsActive" type="checkbox" name="IsActive" style="display: none;"> <img src="/ABC30/Content/Images/TriStateCheckbox/checked.gif"> <input type="hidden" name="IsActive" value="checked"> <label for="chkIsActive">User is active</label> </td> </tr>Alda
Does the value="checked" getting changed to value="unchecked"(or some other) while toggling the checkbox?Karlkarla
in the html it change the code inplace of "Checked.gif" or "Unchecked.gif".Alda
So, with the img src you can check whether it is selected or no, based on that you can click the checkboxKarlkarla
A
4
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!

Anabal answered 20/8, 2014 at 17:7 Comment(7)
It shows the null point exception at the place of if() condition and I manage it via try catch but I think its not a good practice. So, can you please let me know what I do for this??Alda
There is no id given in the HTML for check box, only image src is there which written in the tag <td>--- </td>Alda
I think this is the element I am pointing to: <input id="chkIsActive" type="checkbox" name="IsActive" style="display: none;">. Anyways, can you please run the following code and paste the output here: WebElement chBox = driver.findElement(By.xpath("/html/body/div[3]/div[2]/form/fieldset/div[1]/table/tbody/tr[10]/td/img")); System.out.println(chBox.getAttribute("src")); Please run it twice before and after selecting the checkbox and paste the output here.Anabal
When I run it before and after selecting the checkbox then its out come "Null".Alda
When I check it via some logic in the console it show three checkbox but there are only two checkboxes one for checked.gif and second for unchecked.gif.... I don't understand how to proceed now can you please suggest?Alda
Please post the HTML for the full page and also if possible post two screenshots pre and post selection of the check-box.Anabal
I dont post the full html code here but posted a code for that particular checkbox and hope it would be helpful. <td align="left" colspan="2"> <input id="chkIsActive" type="checkbox" name="IsActive" style="display: none;"> <img src="/A30/Content/Images/Checkbox/checked.gif"> <input type="hidden" name="IsActive" value="checked"> <label for="chkIsActive">User is active</label> </td>Alda
I
1
       WebElement chck = driver.findElement(By.id("chkRemeberMe"));

        if(!chck.isSelected())
        {                 
                System.out.println(" CheckBox Not Selected");
        }
        else
        {
            System.out.println("CheckBox Already Selected");
        }
Instinctive answered 17/6, 2016 at 5:8 Comment(1)
Please add some explanation to answer it will help to understand it better.Calycle
P
1

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.

Pencil answered 3/5, 2019 at 14:7 Comment(0)
R
0

If all else fails, try the is isDisplayed()

driver.findElement(By.xpath(xpath).isDisplayed()
Righteousness answered 5/7, 2018 at 7:48 Comment(0)
M
0

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

Monegasque answered 25/7, 2018 at 1:48 Comment(0)
S
0

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

Sandfly answered 6/11, 2018 at 19:21 Comment(0)
M
0
boolean select = driver.findElementByXPath("xpath").isSelected();
Assert.assertEquals(select, "true");

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

Mccracken answered 21/3, 2022 at 7:46 Comment(0)
K
0

When isselect() Always Returns False do this

 WebElement chkbox =  sc.findElement(By.xpath("(//*[name()='circle'])[5]"));
      
      
      if(!chkbox.isSelected())
      {
          chkbox.click();
          
          System.out.println("True");
      }
      else {
          System.out.println("false");
      }
        
Karns answered 4/8, 2023 at 17:40 Comment(0)
M
0

On python using image of element to find if it is checked.

import PIL
import io

chBox= driver.find_elements(by=By.XPATH,value="/html/body/div[3]/div[2]/form/fieldset/div[1]/table/tbody/tr[10]/td/img")#find checkbox
image_binary = chBox .screenshot_as_png#get raw image of checkbox
img = PIL.Image.open(io.BytesIO(image_binary))#get pil image from raw image
colors=img.getcolors()#list pixels count an color
for color in colors:
    if color[1]==(40, 97, 205, 255):#find blue
        print("checkbox blue")
        break
Meaty answered 18/3, 2024 at 11:17 Comment(0)
G
-1

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
Gumbotil answered 20/8, 2014 at 3:36 Comment(1)
This answer is not relevant to the question.Karlkarla

© 2022 - 2025 — McMap. All rights reserved.