How to Check if the Radio Button Is Selected or Not?
Once we have successfully located the radio button we now need to check if the radio button is Selected or not.
Selenium has special methods to check if the radio button is selected or not, they are-
- is_selected()- is_selected() is a method in Selenium used to check if an element is selected or not. It returns a Boolean value.
Python
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() url = "http://127.0.0.1:5500/index.html" driver.get(url) try : radioButton = driver.find_element(By.NAME, "q1" ) except : print ( "Not Found" ) else : if (radioButton.is_selected()): print ( "element is already selected" ) else : print ( "element is not selected" ) |
Output:
As the element was not selected by default so it prints “element is not selected“
Selenium Handling Radio Buttons
Table of Content
- 1. How to Locate a Radio Button
- 2. How to Check if the Radio Button Is Selected or Not?
- 3. How to Interact with Radio Buttons
Selenium is one of the most popular and widely used tools when it comes to automating web applications and testing web applications. Selenium allows developers and QA testers to automate certain tasks in web applications like filling out forms navigating to a certain page or testing if the functions are working as expected or not. Selenium helps in conducting automation tasks and automating web applications easily.
What is a Radio Button?
One of the most common tasks while interacting with the web application is handling radio buttons. Radio Buttons are the elements on the web page that allow the users to select a select option from a list and allow the users to select an option from the list of options.
Why are radio Buttons used?
Radio buttons are used to provide choices to the user, allowing them to pick a choice from the set of choices. For example, a user may be asked to state their gender from the list of genders, and he has to select his gender from the list of genders.
There are three steps to handle a Radio Button in Selenium they are-
- Locate a Radio Button.
- Check if the Radio Button is selected or not.
- Select a Radio Button.
Contact Us