Working with RadioButtons using Python Selenium Webdriver
Radio buttons are similar to checkboxes. But allows selection of only one value in a group of values.
Radio button operation is easy to perform, we just need to find the radio button element using any one of the techniques explained here, and click on it.
Radio button does not support deselection. To deselect a radio button, one needs to select any other radio button in that group.
What kind of operations we may want perform on a radio button?
1. We might want to validate whether the radio button is selected or not.
2. We just want to select the radio button.
For example, we have a login form with a radio group having two buttons
<html> <body> <form id='loginForm'> Username: <input name='username' type='text'/><br/> Password: <input name='password' type='password'/><br/> Account Type: <input id='savingsaccount' type='radio'>Savings Account</input> <input id='currentaccount' type='radio'>Current Account</input><br/> <input type='button' value='Login'/> </form> </body> </html>
We know how to open Firefox browser from previous posts.
To verify whether a given element identified by the locator is radio button or not, we can do like this
if driver.find_element_by_id("savingsaccount").get_attribute("type") == "radio": print("Element is a Radio button") else: print("Element is not a Radio button")
To click on the ‘Savings Account’ radio button, we need to find the element first and click on it.
radioElement = driver.find_element_by_id("savingsaccount") radioElement.click()
But we are not sure, whether the radio button is selected or not after this click operation.
To verify or get selection status, we can use two mechanisms
1.
driver.find_element_by_id("savingsaccount").is_selected()
This will return ‘true’ if the radio button is selected, false if it is not selected.
2.
driver.find_element_by_id("savingsaccount").get_attribute("checked")
This will return ‘true’ if the radio button is selected. But will return NoneType if radio button is not selected.
Example Code:
from selenium import webdriver driver = webdriver.Firefox(executable_path="[driver path]/geckodriver") driver.get("[URL to open]") # To simply click on radio button regardless of the status driver.find_element_by_id("savingsaccount").click() # To find whether the element is a radio button or not if driver.find_element_by_id("savingsaccount").get_attribute("type") == "radio": print("Element is a radio button") else: print("Element is not a radio button") # To find whether the radio button element is selected or not isSelected = driver.find_element_by_id("savingsaccount").get_attribute("checked") if isSelected is not None: print("Element checked - ", isSelected) else: print("Element checked - false") # To find whether the radio button element is selected or not result = driver.find_element_by_id("savingsaccount").is_selected() print("radio button status - ", result) # Select radio button. This check is not mandatory result = driver.find_element_by_id("savingsaccount").is_selected() if result: print('radio button already selected') else: driver.find_element_by_id("savingsaccount").click() print('radio button selected')
One Response
Possible duplicate of Selenium WebDriver throws Exception in thread main org.openqa.selenium.ElementNotInteractableException: Element is not visible error DebanjanB Feb 12 ’18 at 12:15