Working with checkboxes using Python Selenium Webdriver

The Blog To Learn Selenium and Test Automation

Working with checkboxes using Python Selenium Webdriver

Often times we encounter scenarios to work with Checkboxes using python selenium.
CheckBox operations are easy to perform, we just need to find the checkbox element using any one of the techniques explained here, and click on it.

It is not always straight forward anyway. Because we may need to select checkbox only when it is not selected already; Like wise checkbox deselection, we want deselect checkbox only when it is selected.

To select, if we perform click operation on already selected checkbox, then the checkbox will be deselected; Something we don’t want to happen.
So we need to validate whether the checkbox is selected or not.

Sometimes we just need to know the selection status of the checkbox.

For example, we have a signup form with two checkboxes

<html>
    <body>
      <form id='sigupForm'>
          Username: <input name='username' type='text'/><br/>
	  Password: <input name='password' type='password'/><br/>
	  <input id='privacypolicy' name='privacypolicy' type='checkbox'>Accept Privacy Policy</input><br/>
	  <input id='termsconditions' name='termscondition' type='checkbox'>Accept Terms and Condition</input><br/>
	  <input type='button' value='Submit'/>
      </form>
    </body>
</html>

We know how to open Firefox browser from previous posts.

To verify whether a given element identified by the locator is checkbox or not, we can do like this

if driver.find_element_by_id("privacypolicy").get_attribute("type") == "checkbox":
    print("Element is a checkbox")
else:
    print("Element is not a checkbox")

To click on the ‘Accept Privacy Policy’ checkbox, find the element first and click on it.

checkboxElement = driver.find_element_by_id("privacypolicy")
checkboxElement.click()

But we are not sure, whether this checkbox is selected or not after this click operation.

To verify or get selection status, we can use two mechanisms
1.

driver.find_element_by_id("privacypolicy").is_selected()

This will return ‘true’ if the checkbox is selected, false if it is not selected.

2.

driver.find_element_by_id("privacypolicy").get_attribute("checked")

This will return ‘true’ if the checkbox is selected. But will return NoneType if checkbox is not selected.

Example Code:

from selenium import webdriver

driver = webdriver.Firefox(executable_path="[driver path]/geckodriver")
driver.get("[URL to open]")

# To simply click on checkbox regardless of the status
driver.find_element_by_id("privacypolicy").click()

# To find whether the element is a checkbox or not
if driver.find_element_by_id("privacypolicy").get_attribute("type") == "checkbox":
    print("Element is a checkbox")
else:
    print("Element is not a checkbox")

# To find whether the checkbox element is selected or not
isChecked = driver.find_element_by_id("privacypolicy").get_attribute("checked")
if isChecked is not None:
    print("Element checked - ", isChecked)
else:
    print("Element checked - false")

# To find whether the checkbox element is selected or not
result = driver.find_element_by_id("privacypolicy").is_selected()
print("Checkbox status - ", result)

# Select checkbox only when it is not selected 
result = driver.find_element_by_id("privacypolicy").is_selected()
if result:
    print('Checkbox already selected')
else:
    driver.find_element_by_id("privacypolicy").click()
    print('Checkbox selected')

# Deselect checkbox only when it is selected 
result = driver.find_element_by_id("privacypolicy").is_selected()
if result:
    driver.find_element_by_id("privacypolicy").click()
    print('Checkbox deselected')
else:
    print('Checkbox already deselected')

 

Leave a Reply

Your email address will not be published. Required fields are marked *