Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Handling checkboxes and radio buttons in Selenium with Java involves locating these elements on a web page and then performing actions such as checking, unchecking, and verifying their states. Here’s a detailed explanation with examples for both checkboxes and radio buttons:

Handling Checkboxes

Checkboxes can be checked or unchecked. You can manipulate these states using Selenium.

Example HTML for Checkboxes:

				
					<form>
  <input type="checkbox" id="checkbox1" name="option1" value="A"> Option A<br>
  <input type="checkbox" id="checkbox2" name="option2" value="B"> Option B<br>
  <input type="checkbox" id="checkbox3" name="option3" value="C"> Option C<br>
</form>

				
			

Selenium Code for Checkboxes:

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class CheckboxExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("file:///path/to/checkbox_example.html");

        // Locate the checkbox using its ID
        WebElement checkbox1 = driver.findElement(By.id("checkbox1"));
        
        // Check if the checkbox is selected
        if (!checkbox1.isSelected()) {
            // Check the checkbox
            checkbox1.click();
        }

        // Locate another checkbox and uncheck if it is selected
        WebElement checkbox2 = driver.findElement(By.id("checkbox2"));
        if (checkbox2.isSelected()) {
            checkbox2.click();
        }

        // Verify if the checkbox is checked
        boolean isChecked = checkbox1.isSelected();
        System.out.println("Checkbox 1 is selected: " + isChecked);

        driver.quit();
    }
}

				
			

Handling Radio Buttons

Radio buttons allow the user to select one option from a set. You can select and verify the state of radio buttons similarly to checkboxes.

Example HTML for Radio Buttons:

				
					<form>
  <input type="radio" id="radio1" name="group1" value="A"> Option A<br>
  <input type="radio" id="radio2" name="group1" value="B"> Option B<br>
  <input type="radio" id="radio3" name="group1" value="C"> Option C<br>
</form>

				
			

Selenium Code for Radio Buttons:

				
					import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class RadioButtonExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("file:///path/to/radio_button_example.html");

        // Locate the radio button using its ID
        WebElement radio1 = driver.findElement(By.id("radio1"));
        
        // Select the radio button
        if (!radio1.isSelected()) {
            radio1.click();
        }

        // Locate another radio button
        WebElement radio2 = driver.findElement(By.id("radio2"));
        
        // Verify if the radio button is selected
        boolean isRadio1Selected = radio1.isSelected();
        boolean isRadio2Selected = radio2.isSelected();
        
        System.out.println("Radio 1 is selected: " + isRadio1Selected);
        System.out.println("Radio 2 is selected: " + isRadio2Selected);

        driver.quit();
    }
}

				
			

Key Points

  1. Locating Elements: Use locators like ‘By.id‘, ‘By.name‘, ‘By.xpath‘, etc., to locate checkboxes and radio buttons.
  2. Checking State: Use the ‘isSelected()‘ method to check if a checkbox or radio button is selected.
  3. Performing Actions: Use the ‘click()‘ method to check/uncheck a checkbox or select a radio button.
  4. Verification: Always verify the state after performing actions to ensure the correctness of your test.

Advanced Tips

  • Use ‘WebDriverWait‘ for waiting for elements instead of ‘Thread.sleep ‘or implicit waits for better handling of dynamic content.
  • Use CSS selectors or XPath for more complex locators.
  • Group related radio buttons using ‘name‘ attribute and handle them collectively if needed.

These examples cover the basic interactions with checkboxes and radio buttons using Selenium in Java. You can expand upon these basics to handle more complex scenarios and enhance your test automation scripts.

Scroll to Top