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

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();
2. Navigating to the Website:
driver.get("https://www.singaporeair.com/en_UK/in/home#/managebooking"); driver.manage().window().maximize();
3. Locating Radio Buttons:
WebElement radio1 = driver.findElement(By.id("eticketNumberRadio")); WebElement radio2 = driver.findElement(By.id("bookReferenceRadio"));
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
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());
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.