In this tutorial, we are going to learn how to select a radio button in Selenium WebDriver and we will also learn how to verify that the radio button is enabled and visible or not. Let's first know what is radio button in selenium?

Radio Button in Selenium


Radio button is a type of button in the web application which offers the user a way to select one option from a set of items. If you select an option then the circle is filled and other circles are deselected. If you do not select an option, the circle is empty. Below is an example of radio buttons in the screenshot that shows what the radio button looks like in your browser. 
Radio button in Selenium
In the above example, you will notice that you can only select one radio button option at a time.

How to Select Radio Button in Selenium WebDriver?


Selenium WebDriver provides support for radio buttons through the WebElement class. Here are some methods or functions provided by Selenium WebDriver so that we can interact and select radio button elements during the execution of test script.

1. boolean isSelected(): This method will be used to check that if an element is selected on the web page or not. It returns a boolean value (true) if selected, else false for deselected. It can be executed only on a  radio button, checkbox, and so on.

2. void click(): This method is used to perform to click on a visible element. When we click on an element, a new page is loaded. 

Scenario to be automated:
1. Launch the web browser and open the application under test – https://seleniumtesting08.blogspot.com/2019/08/radio-buttons.html
2. Click on the radio button "Yellow" and display a message that will indicate radio button is successfully selected.
3. Select on radio button "Red" by deselecting "Yellow" radio button.
4. Click on radio button "Dhanbad".
5. Close browser.
6. Compile and run test script. Let's see the following code:
Program source code 1:
    package practiceRadioButton; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class RadioButton { public static void main(String[] args) { // Declare WebDriver reference and assign an object of FirefoxDriver class. WebDriver driver=new FirefoxDriver(); // Maximize window. driver.manage().window().maximize(); String URL="https://seleniumtesting08.blogspot.com/2019/08/radio-buttons.html"; driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate radio button "Yellow". WebElement yellow=driver.findElement(By.xpath("//input[@name='yellow']")); // Click on Radio button yellow to select. yellow.click(); System.out.println("Radio button Yellow is successfully selected."); // The above two lines of code can be written also in just one step like this: // driver.findElement(By.xpath("//input[@name='yellow']")).click(); // Locate radio button "Red". WebElement red=driver.findElement(By.xpath("//input[@name='red']")); // Radio Button Yellow is de-selected and Radio Button Red is selected. red.click(); System.out.println("Radio Button Red is successfully Selected"); // Locate radio button "Dhanbad". WebElement dhanbad=driver.findElement(By.xpath("//input[@name='dhanbad']")); dhanbad.click(); System.out.println("Radio button Dhanbad is successfully selected."); driver.close(); } }
    Output: Radio button Yellow is successfully selected. Radio Button Red is successfully Selected Radio button Dhanbad is successfully selected.

How to check Radio Button is selected or not in Selenium WebDriver?


Let's automate a scenario where we will check that the radio button is enabled and visible or not. When the radio button is visible, we will check that it has been selected or not.

Scenario to be automated:
1. Launch the web browser and open the application under test – https://seleniumtesting08.blogspot.com/2019/08/radio-buttons.html
2. Locate the radio button.
3. Verify that the radio button "Green" is enabled or not.


4. If radio button is enabled and visible, check that it is not selected before clicking on it. We can select a radio button by clicking on it after it has been selected.
5. Compile and run the test script.
Program source code 2:
    package practiceRadioButton; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class RadioButtonTest { public static void main(String[] args) { // Declare WebDriver reference. WebDriver driver; // Create an object of FirefoxDriver class. driver=new FirefoxDriver(); // Maximize browser. driver.manage().window().maximize(); String URL="https://seleniumtesting08.blogspot.com/2019/08/radio-buttons.html"; driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locate the radio button "Green". WebElement green=driver.findElement(By.xpath("//input[@name='green']")); // Verify that radio button "Green" is enabled and visible. if(green.isEnabled() && green.isDisplayed()) { // Using System.out.println method, we will display a message that will indicate whether the verification was successful or not. System.out.println("Radio button is enabled and visible"); } else { System.out.println("Radio button is disabled and invisible"); } // Verify that radio button is not selected before clicking on it. if(!green.isSelected()){ green.click(); // Verify that "Green" option has been selected. if(green.isSelected()) { // Use System.out.println method to display a message that will indicate whether verification is successful or not and the "Green" option has been selected or not. System.out.println("'Green' option is selected"); } else { System.out.println("Something is wrong, 'Green' option is not selected"); } } driver.close(); } }
    Output: Radio button is enabled and visible 'Green' option is selected
Final words
Hope that this tutorial has covered almost all important points related to how to select radio button in selenium webdriver? I hope that you will have understood clearly and enjoyed it practically.
Thanks for reading!