0
0
Selenium Pythontesting~5 mins

Checkbox interactions in Selenium Python

Choose your learning style9 modes available
Introduction

Checkboxes let users select options. Testing them ensures they work right and save user choices.

When you want to check if a checkbox can be selected or deselected.
When testing forms that have multiple options to choose from.
When verifying that the checkbox state is saved after submitting a form.
When automating user actions that involve toggling settings on or off.
When ensuring that only allowed checkboxes can be checked or unchecked.
Syntax
Selenium Python
checkbox = driver.find_element(By.CSS_SELECTOR, 'input[type="checkbox"]')
checkbox.click()

# To check if checkbox is selected
is_checked = checkbox.is_selected()

Use click() to toggle the checkbox state.

Use is_selected() to check if the checkbox is currently checked.

Examples
Find checkbox by ID and click it to select or deselect.
Selenium Python
checkbox = driver.find_element(By.ID, 'subscribe')
checkbox.click()
Check if checkbox is not selected, then click to select it.
Selenium Python
checkbox = driver.find_element(By.NAME, 'terms')
if not checkbox.is_selected():
    checkbox.click()
Select all checkboxes on the page that are not already selected.
Selenium Python
checkboxes = driver.find_elements(By.CSS_SELECTOR, 'input[type="checkbox"]')
for cb in checkboxes:
    if not cb.is_selected():
        cb.click()
Sample Program

This script opens a simple page with one checkbox. It prints if the checkbox is selected before and after clicking it twice.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

# Setup Chrome driver (adjust path as needed)
options = Options()
options.add_argument('--headless')  # Run without opening browser window
service = Service()
driver = webdriver.Chrome(service=service, options=options)

try:
    # Open a simple page with a checkbox
    driver.get('data:text/html,<html><body><form><input type="checkbox" id="agree" name="agree">I agree</form></body></html>')

    checkbox = driver.find_element(By.ID, 'agree')

    # Initially checkbox should not be selected
    print(f'Initially selected: {checkbox.is_selected()}')

    # Click checkbox to select it
    checkbox.click()
    print(f'After click selected: {checkbox.is_selected()}')

    # Click again to deselect
    checkbox.click()
    print(f'After second click selected: {checkbox.is_selected()}')

finally:
    driver.quit()
OutputSuccess
Important Notes

Always locate checkboxes using stable locators like ID or name for reliability.

Use is_selected() to verify checkbox state instead of guessing.

Clicking a checkbox toggles its state: checked to unchecked or vice versa.

Summary

Checkboxes let users pick options; testing ensures they work correctly.

Use click() to change checkbox state and is_selected() to check it.

Choose good locators like ID or name for finding checkboxes in tests.