
📌How to Select Radio Buttons:
1.Enhance your Selenium skills with real-world examples of checkbox and radio button selection.
2.Write robust Selenium scripts to check, uncheck, and validate checkbox states.
3.Handle dynamic checkboxes and radio buttons in Selenium with expert techniques.
4.Learn XPath and CSS selectors to locate radio buttons and checkboxes precisely.
5.Ensure seamless UI testing by automating checkbox and radio button interactions in Selenium.
6.Boost your automation testing skills with a comprehensive Selenium tutorial on checkboxes and radio buttons.
Link Text & Partial Link Text:-
📌How to Select Radio Buttons and Checkboxes in Selenium WebDriver:
Selenium WebDriver is widely used for automating web applications for testing purposes. Interacting with radio buttons and checkboxes is a crucial part of form automation. This guide will cover how to handle these elements using Selenium with Java, including example code and troubleshooting tips.
📌Selecting a Radio Button in Selenium:
Radio buttons are elements that allow users to select one option from multiple choices. In Selenium, you can interact with radio buttons using the click()
method. Let’s see an example:
📌Example Code to Select a Radio Button:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class RadioButtonExample {
public static void main(String[] args) {
// Set up the WebDriver
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Navigate to the webpage
driver.get("https://demo.guru99.com/test/radio.html");
// Locate radio buttons
WebElement radio1 = driver.findElement(By.id("vfb-7-1"));
WebElement radio2 = driver.findElement(By.id("vfb-7-2"));
// Select first radio button
radio1.click();
System.out.println("Radio Button Option 1 Selected");
// Select second radio button
radio2.click();
System.out.println("Radio Button Option 2 Selected");
// Close browser
driver.quit();
}
}
📌Selecting a Checkbox in Selenium:
Checkboxes allow users to select multiple options simultaneously. Like radio buttons, checkboxes can be selected using the click()
method. The isSelected()
method helps verify whether a checkbox is selected or not.
📌Example Code to Select a Checkbox:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CheckboxExample {
public static void main(String[] args) {
// Set up the WebDriver
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Navigate to the webpage
driver.get("https://demo.guru99.com/test/radio.html");
// Locate the checkbox
WebElement checkbox = driver.findElement(By.id("vfb-6-0"));
// Click to select the checkbox
checkbox.click();
System.out.println("Checkbox Selected");
// Verify if the checkbox is selected
if (checkbox.isSelected()) {
System.out.println("Checkbox is checked");
} else {
System.out.println("Checkbox is unchecked");
}
// Close browser
driver.quit();
}
}
📌Handling Checkboxes with Toggle Effect:
Some checkboxes, such as the Facebook “Keep me logged in” option, require checking and unchecking functionality. The following example demonstrates how to handle such scenarios:
📌Example Code for Toggle Checkboxes:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ToggleCheckboxExample {
public static void main(String[] args) {
// Set up the WebDriver
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Navigate to the Facebook page
driver.get("https://demo.guru99.com/test/facebook.html");
// Locate the checkbox
WebElement persistBox = driver.findElement(By.id("persist_box"));
// Click twice to toggle the checkbox
for (int i = 0; i < 2; i++) {
persistBox.click();
System.out.println("Facebook Checkbox Status: " + persistBox.isSelected());
}
// Close browser
driver.quit();
}
}
📌Troubleshooting Selenium Issues:
- Discover the best practices for selecting and verifying checkboxes in Selenium WebDriver.
- Automate form testing by interacting with radio buttons and checkboxes using Selenium.
1. NoSuchElementException:
- Ensure the correct locator (ID, XPath, CSS Selector) is used.
- Check whether the element is present in the DOM before Selenium interacts with it.
- Use explicit waits to handle elements that load dynamically.
2. Element Not Clickable:
- Ensure the element is visible and enabled.
- Use JavaScript Executor to click on elements if needed:
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].click();", element);
3. Handling Dynamic Elements:
- Use dynamic XPath locators:
WebElement dynamicElement = driver.findElement(By.xpath("//input[contains(@id,'dynamic-id-part')]");
📌How to Select Radio Buttons Summary Table:
Element Type | Command | Description |
---|---|---|
Radio Button | click() | Selects the radio button |
Checkbox | click() | Toggles the checkbox |
Checkbox | isSelected() | Verifies if the checkbox is checked |
📌How to Select Radio Buttons Conclusion:
Handling radio buttons and checkboxes in Selenium WebDriver is simple using the click()
and isSelected()
methods. Always use proper locators and add waits where necessary to handle dynamic elements efficiently. By following these best practices, you can automate form testing with ease and improve your Selenium scripts’ reliability.