Handling Radio Button in Selenium Web Driver

 One of the easiest elements to find and automate using Selenium Web Driver is radio buttons. To ensure that the given web element receives the necessary action correctly, you must check the states of the web elements. Each web element has unique qualities or traits. Additionally, it provides a particular web element state and verifies it while conducting UI automation using Selenium Web Driver. "Selenium Radio Button" achieves that exactly. Look for the best online selenium courses to learn more about Selenium-related codes and functions.

What is a radio button?

An HTML element known as a radio button allows the user to choose just one of the available alternatives. Generally, you can organize radio button rallies in a collection of mutually exclusive alternatives. In other words, you can only choose one choice from the list. In HTML, a radio button is defined by the input tag and the attribute "type," which has the value "radio." Therefore, any locator method that uses the DOM to locate and identify elements will employ the input tag to identify the radio buttons.

How to verify the radio button selection using Selenium isSelected()?

How can you use the Selenium isSelected() method to check whether the Radio Button is selected? The isSelected() method can be used to verify whether the radio button is currently chosen or not. It applies to both pre-and post-validation. For instance, if the radio button is not already selected, we can click on it. 

When this code is executed, it will first determine whether the radio button is chosen or not. The returned value will then be checked to see if it is true or false using an ‘if’ condition. If the statement is false, the radio button will not be selected. The radio button will also be selected, and the code inside the ‘if’ condition will run.

Example

/** 

* Validate Radio button using isSelected() methiod 

*/  

WebElement radioElement = driver.findElement(By.id("impressiveRadio")); 

boolean selectState = radioElement.isSelected();                  

//performing click operation only if the element is not selected 

if(selectState == false) {        

radioElement.click(); }

Using Boolean isSelected()

The Boolean isSelected() method determines whether or not a web page element is chosen. If it is selected, it produces a Boolean value as true, or else it returns false. It can only be used on radio buttons and checkboxes.

How can the Selenium isDisplayed() method be used to check whether the Radio Button is displayed?

Now try to construct some straightforward Selenium code to check whether or not a particular radio button appears. We will click and choose the radio button if it is present. 

Example

/** 

* Validate Radio button using isDisplayed() methiod 

*/  

WebElement radioElement = driver.findElement(By.id("impressiveRadio")); 

boolean selectState = radioElement.isDisplayed();                

//performing click operation only if the element is not selected 

if(selectState == false) { radioElement.click(); }

The previously stated test will verify whether the specified radio button appears on the website or not. If displayed, it will decide which one to choose. As mentioned above, the outcome of the code will be identical to what "isSelected()" produced.

How can you use the Selenium isEnabled() method to see if the Radio Button is enabled?

Consider that you wish to click a radio button, but it might be disabled while the program runs. Selenium includes a method called "isEnabled()" to deal with similar situations. This function verifies whether or not the specified web element is enabled. This method will return the Boolean value according to the element's status.                

                           13.Radio buttons in disabled state.png       

You can see a disabled radio button labelled "No" in the image above. The following selenium code must be used to validate this element.

/** 

* Validate Radio button using isEnabled() methiod 

*/  

WebElement radioElement = driver.findElement(By.id("noRadio")); 

boolean selectState = radioElement.isEnabled();                   

//Click on an element only when it is not chosen 

if(selectState == false) { radioElement.click();  

 }

Bottom Line

So what are you waiting for? Get the selenium certification training by enrolling in selenium courses in online training platform. There is high demand for this course in online training classes. 


Comments

Popular posts from this blog

5 Reasons Why Selenium Is the Perfect Tool for Agile Teams

The Future of Selenium: What to Expect in 2023