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?

8
  • Please post html of the element Commented Aug 20, 2014 at 3:36
  • Html of element isnt need the java is wrong Commented Aug 20, 2014 at 3:38
  • @mfsi_sitamj Below are the html: <img src="/ABC30/Content/Images/TriStateCheckbox/checked.gif"> Commented Aug 20, 2014 at 3:48
  • 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? Commented Aug 20, 2014 at 3:53
  • @bcar exactly what its reason? Commented Aug 20, 2014 at 5:12

11 Answers 11

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

Sign up to request clarification or add additional context in comments.

4 Comments

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>
Does the value="checked" getting changed to value="unchecked"(or some other) while toggling the checkbox?
in the html it change the code inplace of "Checked.gif" or "Unchecked.gif".
So, with the img src you can check whether it is selected or no, based on that you can click the checkbox
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!

7 Comments

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??
There is no id given in the HTML for check box, only image src is there which written in the tag <td>--- </td>
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.
When I run it before and after selecting the checkbox then its out come "Null".
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?
|
1
       WebElement chck = driver.findElement(By.id("chkRemeberMe"));

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

1 Comment

Please add some explanation to answer it will help to understand it better.
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.

Comments

0

If all else fails, try the is isDisplayed()

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

Comments

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

Comments

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

Comments

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

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

Comments

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");
      }
        

Comments

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

Comments

-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

1 Comment

This answer is not relevant to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.