How to automate Radio Button in Selenium WebDriver

HOME

Let’s go through the scenario below:-

1) Launch Chrome Browser
2) Maximize the current window
3) Implicitly wait for 5 sec
4) Open browser – https://demo.automationtesting.in/Register.html
5) Find the locator of all Radio Buttons
6) Find the number of Radio Buttons available
7) Print the name of the first option of the radio button
8) Select the first option of the radio button
9) Print the name of the second option of the radio button
10) Select the second option of the radio button
11) Close the browser

    ChromeOptions options = new ChromeOptions();
    WebDriver driver = new ChromeDriver(options);
    
    driver.manage().window().maximize();
    
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    driver.get("https://demo.automationtesting.in/Register.html");
    
    List<WebElement> Radio_Options = driver.findElements(By.xpath("//*[@name='radiooptions']"));
    
    int radioSize = Radio_Options.size();
    System.out.println("No Of Radio Button Options :" + radioSize);
    
    for (int i = 0; i < radioSize; i++) {
        System.out.println("Name of Radio Button :"+ Radio_Options.get(i).getAttribute("Value"));
        Radio_Options.get(i).click();
        System.out.println("Radio Button Option "+ (i+1) +" is selected");
    }
    
    driver.quit();
    

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    
    import java.time.Duration;
    import java.util.List;
    
    public class RadioButton_Demo {
    
        public static void main(String[] args) throws InterruptedException {
    
            // Initiate Chrome browser
            ChromeOptions options = new ChromeOptions();
            WebDriver driver = new ChromeDriver(options);
    
            // Maximize the browser
            driver.manage().window().maximize();
    
            // Put an Implicit wait and launch URL
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
            driver.get("https://demo.automationtesting.in/Register.html");
    
            // Find locator of all Radio Buttons
            List<WebElement> Radio_Options = driver.findElements(By.xpath("//*[@name='radiooptions']"));
    
            // Find no of Radio Buttons available and print their values
            int radioSize = Radio_Options.size();
            System.out.println("No Of Radio Button Options :" + radioSize);
    
            Thread.sleep(5000);
    
            for (int i= 0; i< radioSize; i++) {
                System.out.println("Name of Radio Button :"+ Radio_Options.get(i).getAttribute("Value"));
                Radio_Options.get(i).click();
                System.out.println("Radio Button Option "+ (i+1) +" is selected");
            }
            driver.quit()  ;
        }
    }
    

    Leave a comment