Wednesday, November 12, 2025

thumbnail

Handling Checkboxes and Radio Buttons in Selenium

 ⚙️ Prerequisites


Make sure you have:


pip install selenium



And the driver for your browser (e.g., ChromeDriver for Chrome).


Typical setup:


from selenium import webdriver

from selenium.webdriver.common.by import By


driver = webdriver.Chrome()

driver.get("https://example.com")


✅ 1. Handling Checkboxes


Checkboxes allow multiple selections.

You can locate them by their id, name, value, or XPath/CSS Selector.


๐Ÿ”น Example HTML

<input type="checkbox" id="java" name="language" value="Java">

<input type="checkbox" id="python" name="language" value="Python">

<input type="checkbox" id="js" name="language" value="JavaScript">


๐Ÿ”น Example Code

# Locate by ID and click

java_cb = driver.find_element(By.ID, "java")

java_cb.click()


# Check if selected

print(java_cb.is_selected())  # True if checked


# Select multiple

langs = driver.find_elements(By.NAME, "language")

for lang in langs:

    if lang.get_attribute("value") in ["Python", "JavaScript"]:

        lang.click()


๐Ÿง  Notes


.click() toggles the checkbox (checks/unchecks).


Use .is_selected() to verify its state.


You can use conditions to select specific ones.


๐Ÿ”˜ 2. Handling Radio Buttons


Radio buttons allow only one option to be selected in a group.


๐Ÿ”น Example HTML

<input type="radio" id="male" name="gender" value="male">

<input type="radio" id="female" name="gender" value="female">

<input type="radio" id="other" name="gender" value="other">


๐Ÿ”น Example Code

# Select a specific radio button

female_rb = driver.find_element(By.ID, "female")

female_rb.click()


# Verify selection

print(female_rb.is_selected())  # True


# Select by value attribute

genders = driver.find_elements(By.NAME, "gender")

for gender in genders:

    if gender.get_attribute("value") == "male":

        gender.click()


๐Ÿงฉ 3. Common Techniques

✅ Verify Selection State

if not java_cb.is_selected():

    java_cb.click()


✅ Deselect Checkbox (if toggle is allowed)


If the checkbox is already checked:


if java_cb.is_selected():

    java_cb.click()  # unchecks it


✅ Wait for Visibility (Recommended)


Use explicit waits before interacting:


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


java_cb = WebDriverWait(driver, 10).until(

    EC.element_to_be_clickable((By.ID, "java"))

)

java_cb.click()


๐Ÿง  Summary

Action Method

Find checkbox/radio find_element() / find_elements()

Click (select/deselect) .click()

Check if selected .is_selected()

Get attribute value .get_attribute("value")

Wait for clickable WebDriverWait + EC.element_to_be_clickable()

๐Ÿงช Example: Complete Script

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


driver = webdriver.Chrome()

driver.get("https://www.w3schools.com/html/html_forms.asp")


# Example: checkboxes or radios on a sample page

wait = WebDriverWait(driver, 10)


# Example: clicking a checkbox

checkbox = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@type='checkbox']")))

checkbox.click()

print("Checkbox selected:", checkbox.is_selected())


driver.quit()

Learn Selenium with JAVA Training in Hyderabad

Read More

How to Verify Page Title and URL with Selenium

Navigating Between Pages Using Selenium WebDriver

How to Handle Input Forms in Selenium

Working with ChromeDriver, GeckoDriver, and EdgeDriver

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive