Test Overview
This test opens a webpage with checkboxes, clicks a checkbox to select it, and verifies that it is selected.
This test opens a webpage with checkboxes, clicks a checkbox to select it, and verifies that it is selected.
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 import unittest class TestCheckboxInteractions(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/checkboxes') def test_checkbox_select(self): driver = self.driver wait = WebDriverWait(driver, 10) checkbox = wait.until(EC.element_to_be_clickable((By.ID, 'subscribe'))) checkbox.click() self.assertTrue(checkbox.is_selected(), 'Checkbox should be selected after click') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to navigate | — | PASS |
| 2 | Navigates to 'https://example.com/checkboxes' | Page with checkboxes is loaded | — | PASS |
| 3 | Waits until checkbox with ID 'subscribe' is clickable | Checkbox element is found and clickable on the page | — | PASS |
| 4 | Clicks the checkbox to select it | Checkbox is now checked | — | PASS |
| 5 | Checks if the checkbox is selected | Checkbox state is selected | Assert checkbox.is_selected() is True | PASS |
| 6 | Test ends and browser closes | Browser window is closed | — | PASS |
is_selected() method do when used on a checkbox element in Selenium?is_selected()click() which changes state, is_selected() only reads the current state.click().select(), check(), or toggle() do not exist in Selenium WebDriver API.checkbox = driver.find_element(By.ID, 'subscribe') checkbox.click() print(checkbox.is_selected())
is_selected() returns True.checkbox = driver.find_element(By.NAME, 'agree')
if not checkbox.is_selected():
checkbox.click()By.NAME, 'agree'.click() works on checkboxes, and is_selected() only reads state, so these are not the problem.Pass the test and this pattern moves unknown