7

How to determine if a checkbox is check or not through xpath

Currently I am trying :

//input[@type='checkbox' and @checked='true')]

I have multiple checkboxes with same ids so I have to select the next checkbox which is not selected/checked.

Specifically I need this for Selenium IDE

Edit what I actually need is sth like :

|storeXpathCount | //input[@name='xyz' and @checked='checked'] | is_checked | 

if checkbox 'xyz' is checked , is_checked value should be 1 else 0

thanks

sakhunzai
  • 13,900
  • 23
  • 98
  • 159

5 Answers5

14

The xpath

// Xpath, only works in certain situations
//input[@type='checkbox' and @checked='xyz']

only works if in the HTML source of the checkbox there is an attribute as follows

checked="xyz"

where the developer is free to replace "xyz" with anything: "true", "checked", "ischecked", ...

So if there is no such attribute, the above mentioned xpath does not work.

While waiting for more insight, you might consider using a CSS selector, which does not depend on such an attribute:

// CSS selector, for all checkboxes which are checked
input:checked[type='checkbox']

// CSS selector, for all checkboxes which are not checked
input:not(:checked)[type='checkbox']

Note that without

[type='checkbox']

you will be given also radios :-)

CuongHuyTo
  • 1,333
  • 13
  • 19
4

enter image description here

Here stnewrecord is class of check-box. Script will store number of check-box, and according to that loop will follow. It will check whether check-box is checked or not if not it will check else move to next step.

xpath=(//input[@class='stnewrecord'])[${i}]  

This is xpath of check-box. 'i' is position, it will increase on each iteration.

Let me know whether its working for you or not..

Rohit Ware
  • 1,982
  • 1
  • 13
  • 29
3

However, there are solution without using css classes and selectors.

Use

//input[@type='checkbox' and @checked]

or

//input[@type='checkbox' and not(@checked)]

instead of

//input[@type='checkbox' and @checked='xyz']
nichiporets
  • 431
  • 7
  • 18
  • 1
    have you tried this in Selenium IDE ? @checked value is 'on' or 'off' its not a boolean ! – sakhunzai Nov 26 '12 at 04:52
  • I had not tried this in IDE, but I use this expression expression with Selenium extension for PHPUnit. However, I think that xpath engine is the same for both tools. Let me know if you have tried this in IDE. – nichiporets Nov 27 '12 at 15:04
  • yes I have some tests for selenium IDE and it does not return boolean values , instead 'on' , 'off' for @checked attribute – sakhunzai Nov 28 '12 at 05:32
  • I'm necroposting here, but as of 2018, this is wrong. (Try it in Chrome browser.) – Mike Warren Jun 22 '18 at 17:42
0

something like this may works:

//input[@checked='checked']/following-sibling::*[1][not(@checked='checked')]
N4553R
  • 188
  • 4
0

You can try these one:

//*[@type='radio'][@checked='checked']/
//*[@type='radio'][not(@checked='checked')]/
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • thanks, we are strictly talking in the context of Selenium IDE, in that context its `@checked` attirbutes has 'on' or 'off' value , please read the above answers for the history – sakhunzai Sep 17 '13 at 04:56
  • I've tested these in Selenium IDE and it seemed that it was working, but I could be wrong, as I was looking for something similar. – kenorb Sep 17 '13 at 09:35