Last modified: Dec 02, 2023 By Alexander Williams

Python Selenium: Check if Checkbox is Checked - Examples

Example 1: Check if Checkbox is Checked by Attribute


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a checkbox
driver.get('https://example.com/checkbox')

# Find the checkbox element
checkbox = driver.find_element_by_id('example-checkbox')

# Check if the checkbox is checked by checking the 'checked' attribute
if checkbox.get_attribute('checked') is not None:
    print("Checkbox is checked.")
else:
    print("Checkbox is not checked.")

# Close the browser
driver.quit()

Output:


# Checkbox is checked.

Example 2: Check if Checkbox is Checked Using is_selected() Method


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a checkbox
driver.get('https://example.com/checkbox')

# Find the checkbox element
checkbox = driver.find_element_by_id('example-checkbox')

# Check if the checkbox is checked using the is_selected() method
if checkbox.is_selected():
    print("Checkbox is checked.")
else:
    print("Checkbox is not checked.")

# Close the browser
driver.quit()

Output:


# Checkbox is checked.

Example 3: Check if Checkbox is Checked Using get_property()


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a checkbox
driver.get('https://example.com/checkbox')

# Find the checkbox element
checkbox = driver.find_element_by_id('example-checkbox')

# Check if the checkbox is checked using the get_property() method
if checkbox.get_property('checked'):
    print("Checkbox is checked.")
else:
    print("Checkbox is not checked.")

# Close the browser
driver.quit()

Output:


# Checkbox is checked.

Example 4: Check if Checkbox is Checked Using get_attribute()


from selenium import webdriver

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a checkbox
driver.get('https://example.com/checkbox')

# Find the checkbox element
checkbox = driver.find_element_by_id('example-checkbox')

# Check if the checkbox is checked using the get_attribute() method
if checkbox.get_attribute('checked') == 'true':
    print("Checkbox is checked.")
else:
    print("Checkbox is not checked.")

# Close the browser
driver.quit()

Output:


# Checkbox is checked.

Example 5: Check if Checkbox is Checked with Explicit Wait


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage with a checkbox
driver.get('https://example.com/checkbox')

# Define an explicit wait for the checkbox to be clickable
checkbox_locator = (By.ID, 'example-checkbox')
checkbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(checkbox_locator))

# Check if the checkbox is checked
if checkbox.is_selected():
    print("Checkbox is checked.")
else:
    print("Checkbox is not checked.")

# Close the browser
driver.quit()

Output:


# Checkbox is checked.