How to select checkboxes and radio buttons in Selenium webdriver
radio_button_element.click()checkbox_button_element.click()
To select the radio button or checkbox, we just need to call the click() method on that element.
The click() method doesn't take any parameters and returns none. It just performs an action.
Let us take a look at an example of this.
Example
from selenium import webdriver#specify where your chrome driver present in your pcPATH=r"C:\Users\gutkrish\Documents\chromedriver\chromedriver.exe"#get instance of web driverdriver = webdriver.Chrome(PATH)#provide website url heredriver.get("https://omayo.blogspot.com/")#locate radio button and click itdriver.find_element("id","radio2").click()#locate checkbox and click itdriver.find_element("id","checkbox2").click()
Explanation
Line 1: We import the
webdriverfrom theseleniumpackage.Line 5: We will be creating and passing a variable
PATHwith value as a path pointing to the web browser driver. For Chrome, it is thechromedriver.exedriver in the Windows environment.Line 8: We get the instance of the
webdriver.Line 11: We provide the URL to the
driver.get()method to open it in a browser.Line 14: We find the radio button element using its id
radio2and call theclick()method to select it.Line 17: We find the checkbox element using its id
checkbox2and call theclick()method to select it.
Free Resources