Complete the code to select a radio button by its ID.
driver.find_element(By.ID, [1]).click()The radio button with ID "radio1" is selected by using find_element(By.ID, "radio1") and then clicking it.
Complete the code to check if a radio button is selected.
is_selected = driver.find_element(By.NAME, [1]).is_selected()The radio buttons often share the same name attribute like "gender". We check if the radio button with name "gender" is selected.
Fix the error in the code to select a radio button using XPath.
driver.find_element(By.XPATH, [1]).click()The correct XPath selects an input element with type='radio' and value='male'. Other options select wrong elements or attributes.
Fill both blanks to create a function that selects a radio button by its value attribute.
def select_radio_by_value(driver, value): radio = driver.find_element(By.CSS_SELECTOR, "input[type='radio'][value='" + [1] + "']") if not radio.[2](): radio.click()
The function finds the radio button with the given value and clicks it only if it is not already selected using is_selected().
Fill all three blanks to create a test that verifies the correct radio button is selected after clicking.
def test_radio_selection(driver): driver.get("http://example.com/radio") radio_buttons = driver.find_elements(By.NAME, [1]) radio_buttons[[2]].click() assert radio_buttons[[3]].is_selected(), "Radio button not selected as expected"
The test opens the page, finds radio buttons by name "gender", clicks the second radio button (index 1), and asserts it is selected.