The Python code below shows you how to check radio buttons and checkboxes using different Selenium locators.
import time from selenium import webdriver from selenium.webdriver.common.keys import Keys # Create chrome driver. driver = webdriver.Chrome() # Open the webpage. driver.get("https://openwritings.net/sites/default/files/selenium-test-pages/radio_checkbox.html") # Pause for 5 seconds for you to see the initial state. time.sleep(5) # Radio Button: Check Monday using XPATH locator. element = driver.find_element_by_xpath("//input[@value='Mon']") element.click() # Checkbox: Uncheck Apple using CSS selector. element = driver.find_element_by_css_selector("input[name='apple']") element.click() # Checkbox: Check Orange using CSS selector. element = driver.find_element_by_css_selector("input[name='orange']") element.click() # Pause for 10 seconds so that you can see the results. time.sleep(10) # Close. driver.close()
Here is a video showing the code above in action.