Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a checkbox is checked in Selenium Python WebDriver?

I've been searching a week how check if a checkbox is checked in Selenium WebDriver with Python, but I find only algorithms from JAVA. I've read the WebDriver docs and it doesn't have an answer for that.
Anyone have a solution?

like image 987
Júlio Griebeler Avatar asked Jan 21 '13 16:01

Júlio Griebeler


People also ask

How do I check if a checkbox is checked in Python?

PyQt5 – isChecked() method for Check Box isChecked method is used to know if the check box is checked or not. This method will return true if the check box is checked else it will return false. If we use this method after creating the check box it will always return False as by default check box is not checked.

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.

How do I check a box in Selenium?

We can check a checkbox in a page in Selenium with the help of click() method. First of all we need to uniquely identify the checkbox with the help of any of the locators like css, xpath, id, class and so on. Next we have to use findElement() method to locate the element and finally perform the clicking action.

Which method is used to check the status of checkbox?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.


1 Answers

There is a WebElement property called is_selected(), and for a check box this indicates whether or not it is checked. Therefore you can verify if it is checked/unchecked by doing something like this:

driver.find_element_by_name('<check_box_name>').is_selected() 

or

driver.find_element_by_id('<check_box_id>').is_selected() 

I remember having the same issue not being able to find documentation. It's easier to find once you know the name (here are some docs, is_selected is towards the bottom), but the way I have gone about trying to find different options/properties for Selenium objects is to just drop dir(some_object) in the code and see what options come up (this is how is_selected appeared).

like image 128
RocketDonkey Avatar answered Sep 22 '22 21:09

RocketDonkey