Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath to determine checkbox "checked" attribute in Selenium IDE

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

like image 737
sakhunzai Avatar asked Aug 01 '12 11:08

sakhunzai


People also ask

How do you check if the checkbox is checked or not in Selenium?

In order to check if a checkbox is checked or unchecked, we can used the isSelected() method over the checkbox element. The isSelected() method returns a boolean value of true if the checkbox is checked false otherwise.

What is XPath for checkbox?

xpath("//label[text()='Reading']")). click(); Note: In the above code, we have clicked on the label associated with the checkboxes. Generally, checkboxes can be checked/unchecked by clicking both the checkbox itself or the labels associated with the checkboxes.

How do you check if a checkbox is checked or not?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

Which attribute is used to check the checkbox?

Answer. Answer: When rendering a page with a checkbox you want selected or checked by default you need to include the 'checked' attribute.


1 Answers

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 :-)

like image 183
CuongHuyTo Avatar answered Oct 12 '22 22:10

CuongHuyTo