Test Overview
This test opens a webpage with radio buttons, selects a specific radio button, and verifies that it is selected correctly.
This test opens a webpage with radio buttons, selects a specific radio button, and verifies that it is selected correctly.
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 RadioButtonTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/radio-buttons') def test_select_radio_button(self): driver = self.driver wait = WebDriverWait(driver, 10) # Wait until radio button is clickable radio_button = wait.until(EC.element_to_be_clickable((By.ID, 'option2'))) radio_button.click() # Verify the radio button is selected self.assertTrue(radio_button.is_selected(), 'Radio button option2 should be selected') 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 load URL | — | PASS |
| 2 | Navigates to 'https://example.com/radio-buttons' | Page with radio buttons loads fully | — | PASS |
| 3 | Waits until radio button with ID 'option2' is clickable | Radio button 'option2' is visible and interactable | — | PASS |
| 4 | Clicks on radio button with ID 'option2' | Radio button 'option2' is selected | — | PASS |
| 5 | Checks if radio button 'option2' is selected | Radio button 'option2' remains selected | Assert radio_button.is_selected() is True | PASS |
| 6 | Test ends and browser closes | Browser window closed | — | PASS |
is_selected() method do when used on a radio button element in Selenium?is_selected()click() which changes the state, is_selected() only returns a boolean indicating selection status.click() is used to select radio buttons or checkboxes.select(), check(), or choose() do not exist in Selenium WebDriver API.radio1 = driver.find_element(By.ID, 'r1') radio2 = driver.find_element(By.ID, 'r2') radio1.click() print(radio1.is_selected()) print(radio2.is_selected())
radio1, radio1.is_selected() returns True, and radio2.is_selected() returns False.radio = driver.find_element(By.NAME, 'gender') radio.click()
find_element(By.NAME) worksfind_elements(By.NAME, 'color') to get all radio buttons in the group.Pass the test and this pattern moves unknown