logo

The best IT Trainig Institute In Gurgaon

Select Radio Button

Code to Select Radio Button

**write your package Name and Class Name in your code
                
package asc;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import io.github.bonigarcia.wdm.WebDriverManager;

public class radioButton {

public static void main(String[] args) throws InterruptedException {
		

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();

 driver.get("https://www.singaporeair.com/en_UK/in/home#/managebooking");

driver.manage().window().maximize();

WebElement radio1 = driver.findElement(By.id("eticketNumberRadio"));
WebElement radio2 = driver.findElement(By.id("bookReferenceRadio"));

radio1.click();

//driver.findElement(By.xpath("//input[@id='redeemFlights']")).isSelected();

Thread.sleep(2000);

//driver.findElement(By.xpath("//*[@id=\"hwidget\"]/div[2]/div/div[2]/div[1]/div/div/div[3]/div[2]/div[2]/div/div/div[1]/div[2]/label")).click();
radio2.click();

Thread.sleep(2000);

radio1.click();

System.out.println(radio1.isSelected());
System.out.println(radio2.isSelected());
System.out.println(radio1.isSelected());
System.out.println(driver.findElement(By.xpath("//*[@name='manage-book-reference-flight-radio']")).getSize());


}

}

            
output
Output
output

In Selenium WebDriver, radio buttons are a type of input element that allows users to select one option from a set of predefined options. Only one radio button in the group can be selected at any given time. Let's break down our code to understand how it interacts with radio buttons on the webpage:

1. Setup and Initialization:

    
    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
                    
  • This initializes the WebDriver, sets up the ChromeDriver using WebDriverManager, and launches a new Chrome browser instance.

2. Navigating to the Website:

                        
    driver.get("https://www.singaporeair.com/en_UK/in/home#/managebooking");
    driver.manage().window().maximize();
                        
                    
  • The get method is used to navigate to the specified URL, and manage().window().maximize() maximizes the browser window.

3. Locating Radio Buttons:

                        
    WebElement radio1 = driver.findElement(By.id("eticketNumberRadio"));
    WebElement radio2 = driver.findElement(By.id("bookReferenceRadio"));
                        
                    
  • These lines locate two radio buttons on the webpage using their id attributes. radio1 and radio2 represent the elements for the "eticketNumberRadio" and "bookReferenceRadio" radio buttons, respectively.

4. Interacting with Radio Buttons:

                        
    radio1.click(); // Selects the first radio button
    Thread.sleep(2000); // Pauses execution for 2 seconds
    radio2.click(); // Selects the second radio button
    Thread.sleep(2000); // Pauses execution for 2 seconds
    radio1.click(); // Re-selects the first radio button
                        
                    
  • click() is used to select a radio button. When radio1.click() is called, the first radio button is selected. After a 2-second pause, radio2.click() selects the second radio button, and then radio1.click() re-selects the first one.The Thread .sleep(2000) method is used to pause the execution for 2 seconds between these actions, allowing you to observe the changes.

5. Checking Selection Status:

                        
    System.out.println(radio1.isSelected());
    System.out.println(radio2.isSelected());
    System.out.println(radio1.isSelected());
    System.out.println(driver.findElement(By.xpath("//*[@name='manage-book-reference-flight-radio']")).getSize());
                        
                    
  • isSelected() checks if a radio button is selected and returns true or false.
  • The final line attempts to print the size of the element identified by the given XPath, though it seems unrelated to radio button selection.
IMP
  • Radio Button Selection: In a group of radio buttons, selecting one will automatically deselect the others.
  • Element Locators: Use attributes like id, name, xpath, etc., to locate elements.
  • Thread.sleep: Introduces a pause but is generally not recommended for synchronization. Use explicit or implicit waits instead for better control.

This code demonstrates basic radio button interactions in Selenium WebDriver, including selection and state checking.