46

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?

7 Answers 7

104

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

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

1 Comment

Thanks for this. If you find that a checkbox is "checked" and you want to change it to "unchecked", how would this be done?
7

I'm using driver.find_element_by_name("< check_box_name >").is_selected()

1 Comment

How does this differ from the first alternative in RocketDonkey's 2013/01 answer?
6

I found another way that works, but using javascript inside.

def is_checked(self, driver, item_id):
  checked = driver.execute_script(
    f"return document.getElementById('{item_id}').checked"
  )
  return checked

Comments

2
agreed = driver.find_element_by_id("checkBoxAgreed")
if agreed.get_attribute("checked") != "true":
    agreed.click()

Comments

0
WebElement checkbox = driver.findElement(By.id("checkboxId"))

(or)

checkbox = self.driver.find_element_by_css_selector(checkboxSelector)

, then

if checkbox.isSelected():
    print 'Checkbox is selected'

2 Comments

The first alternative looks entirely similar to one in RocketDonkey's 2013 answer.
Replace isSelected with is_selected because in the selenium python api the function name is snake cased. I would make the edit but the SO rules prohibit edits that are less than 6 characters.
0

when the checkbox is unchecked, the xpath or id is different than when it is checked which means, then while finding the element,we need to

  1. find some common text that is present in the xpath in both checked and unchecked condition

  2. use it to locate the object

  3. then compare the xpath of checked checkbox and unchecked checkbox

    if (xpath of checked checkbox): pass else: chkbox.click()

please see this video it will give u an idea

https://www.youtube.com/watch?v=c6a7tAoKIo0

Comments

-3
def assert_checkbox_status (id, expect):
    global browser
    field = browser.find_element_by_id(id)
    assert field.get_attribute ('checked')== expect

Example of use:

assert_checkbox('activate', True) ==> assert if checkbox is checked
assert_checkbox('activate', None) ==> assert if checkbox is unchecked

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.