Checkbox interactions in Selenium Python - Build an Automation 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 # Setup WebDriver with webdriver.Chrome() as driver: driver.get('https://example.com/checkbox-demo') wait = WebDriverWait(driver, 10) # Wait for checkbox to be clickable checkbox = wait.until(EC.element_to_be_clickable((By.ID, 'subscribe-newsletter'))) # Verify checkbox is initially not selected assert not checkbox.is_selected(), 'Checkbox should be initially not selected' # Click to select checkbox checkbox.click() # Verify checkbox is selected assert checkbox.is_selected(), 'Checkbox should be selected after clicking' # Click again to deselect checkbox checkbox.click() # Verify checkbox is not selected assert not checkbox.is_selected(), 'Checkbox should be not selected after second click'
This script uses Selenium WebDriver with Python to automate the checkbox interaction test.
First, it opens the browser and navigates to the checkbox demo page.
It waits explicitly until the checkbox with id 'subscribe-newsletter' is clickable to avoid timing issues.
Then it asserts the checkbox is initially not selected.
Next, it clicks the checkbox to select it and asserts it is selected.
Then it clicks again to deselect and asserts it is not selected.
The use of with statement ensures the browser closes automatically after the test.
Now add data-driven testing with 3 different checkboxes having ids 'subscribe-newsletter', 'accept-terms', and 'receive-updates'. Verify selection and deselection for each.
Practice
is_selected() method do when used on a checkbox element in Selenium?Solution
Step 1: Understand the purpose of
This method returns a boolean indicating if the checkbox is checked or not.is_selected()Step 2: Differentiate from other methods
Unlikeclick()which changes state,is_selected()only reads the current state.Final Answer:
It checks if the checkbox is currently checked (selected). -> Option DQuick Check:
Check checkbox state = is_selected() [OK]
- Confusing is_selected() with click()
- Thinking is_selected() changes checkbox state
- Using is_selected() to find elements
Solution
Step 1: Recall Selenium method for clicking
The standard method to simulate a mouse click on an element isclick().Step 2: Verify other options
Methods likeselect(),check(), ortoggle()do not exist in Selenium WebDriver API.Final Answer:
checkbox.click() -> Option BQuick Check:
Click checkbox = click() method [OK]
- Using non-existent methods like select() or check()
- Trying to assign True/False directly to checkbox
- Confusing click() with is_selected()
checkbox = driver.find_element(By.ID, 'subscribe') checkbox.click() print(checkbox.is_selected())
Solution
Step 1: Understand the code actions
The code finds a checkbox by ID 'subscribe', clicks it to select, then prints if it is selected.Step 2: Predict the checkbox state after click()
Clicking a checkbox toggles it to selected if it was not selected before, sois_selected()returns True.Final Answer:
True -> Option AQuick Check:
Click then check selected = True [OK]
- Assuming is_selected() returns False after click
- Thinking print outputs None
- Expecting an exception without errors in code
checkbox = driver.find_element(By.NAME, 'agree')
if not checkbox.is_selected():
checkbox.click()But the checkbox is never selected after running. What is the likely problem?
Solution
Step 1: Analyze the locator usage
If the checkbox is never selected, the element might not be found correctly byBy.NAME, 'agree'.Step 2: Check method correctness
click()works on checkboxes, andis_selected()only reads state, so these are not the problem.Final Answer:
The locator By.NAME with 'agree' is incorrect or does not find the checkbox. -> Option AQuick Check:
Wrong locator = no click effect [OK]
- Assuming click() doesn't work on checkboxes
- Using non-existent methods like select()
- Thinking is_selected() changes state
Solution
Step 1: Understand the requirement
The checkbox should be checked only if it is not already checked to avoid toggling it off.Step 2: Analyze each option
checkbox = driver.find_element(By.ID, 'terms') if not checkbox.is_selected(): checkbox.click() checks state and clicks only if unchecked, which is correct. checkbox = driver.find_element(By.ID, 'terms') checkbox.click() assert checkbox.is_selected() always clicks, possibly unchecking. checkbox = driver.find_element(By.ID, 'terms') if checkbox.is_selected(): checkbox.click() clicks if already checked, unchecking it. checkbox = driver.find_element(By.ID, 'terms') checkbox.select() uses a non-existent method.Final Answer:
checkbox = driver.find_element(By.ID, 'terms') if not checkbox.is_selected(): checkbox.click() -> Option CQuick Check:
Check state before click to avoid toggling off [OK]
- Clicking checkbox without checking state first
- Using non-existent methods like select()
- Clicking when checkbox is already selected
Ready to prove it?
Pass the test and this pattern moves unknown
