Handling Dropdown Checkbox RadioButton using selenium with Java

Introduction

In this article, we will understand how to handle dropdown, checkbox, and radio buttons using Selenium with Java.

Prerequisite

Need to set up selenium with Java project.

In Html, the select tag is used to create the dropdown

Select tags can contain one or more <option> tags as their children, each representing an item in the dropdown. The <option> tags define the individual options that users can choose from in the dropdown.

In order to handle dropdowns, we need to make use of select classes in Selenium.

Select a class from this package.

org.openqa.selenium.support.ui.Select

Create a select class object.

Pass the reference of the webelement to the select class.

Now use the select class object and call the below functions.

  • selectByVisibleText
  • selectByIndex
  • selectByValue

selectByVisibleText

This method will match with the visible text attribute so that you can click the dropdown value for which it becomes matched.

selectByValue

we need to pass a text as a parameter to select_by_value. Then, it will match with the value attribute text so that the user can click the dropdown value for which it becomes matched.

selectByIndex

Index values start from 0 and continue with an increment of +1. It means if there are five dropdown values, then they will be 0,1,2,3 and 4. Suppose you want to select the 3rd value from the dropdown, then the index will be 2.

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.support.ui.Select;

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

        // Launch the Chrome browser
        WebDriver driver = new ChromeDriver();

        // Navigate to the webpage
        driver.get("<application url which you are testing");

        // Locate the dropdown element
        WebElement dropdownElement = driver.findElement(By.id("dropdownId"));

        // Create a Select object
        Select dropdown = new Select(dropdownElement);

        // Select an option by visible text
        dropdown.selectByVisibleText("Option Text");

        // Select an option by visible text
        dropdown.deselectByIndex(2);
        // Select an option by value
 
        dropdown.deselectByValue("optionValue");


        // Close the browser
        driver.quit();
    }
}

If you want to retrieve all the options from the dropdown

Use the below code.

Retrieve all the options from the dropdown.

//get options returns a list so we are storing the options in the list and looping the list using for loop and printing the all the options using the gettext method which return the text of element of the each webelement.

List<WebElement> allOptions = dropdown.getOptions();
for (WebElement option : allOptions) {
    System.out.println("Option: " + option.getText());
}

To interact with checkboxes using Selenium with Java, you can use the WebElement methods such as click().

Store webelement in a webelement reference

WebElement checkboxElement = driver.findElement(By.id("id of checkbox"));

Use the click method to click the checkbox

// Check the checkbox if it is not already checked
        if (!checkboxElement.isSelected()) {
            checkboxElement.click();
        }

Uncheck the checkbox

// Uncheck the checkbox if it is checked
        if (checkboxElement.isSelected()) {
            checkboxElement.click();
        }

Handling radio buttons in Selenium with Java is similar to handling checkboxes. You use the WebElement methods, particularly the click() method,

Store webelement in an webelement reference

WebElement radioButtonElement = driver.findElement(By.id("id of radiobutton"));
 // Check the radio button if it is not already checked
        if (!radioButtonElement.isSelected()) {
            radioButtonElement.click();
        }

Selenium WebDriver provides certain methods that we can use for pre and post-validation of the states of a CheckBox. A few of these methods are.

  1. isSelected(): Checks whether a checkbox/radio is selected or not.
  2. isDisplayed(): Checks whether a Webelement displays on the web page or not.
  3. isEnabled(): Checks whether a Weblement is in enabled or disabled state or not

All the above methods will return a boolean value true/false.

WebElement checkboxElement = driver.findElement(By.id("checkboxId"));

Check the State with isSelected():
Use the isSelected() method to check whether the form element is selected (checked) or not.

if (checkboxElement.isSelected()) {
    System.out.println("Checkbox is selected.");
} else {
    System.out.println("Checkbox is not selected.");
}
WebElement buttonElement = driver.findElement(By.id("buttonId"));

if (buttonElement.isEnabled()) {
    System.out.println("Button is enabled.");
    // Perform some action like clicking the button
    buttonElement.click();
} else {
    System.out.println("Button is disabled.");
}
WebElement element = driver.findElement(By.id("elementId"));


if (element.isDisplayed()) {
    System.out.println("Element is displayed.");
    // Perform some action with the element
} else {
    System.out.println("Element is not displayed.");
}

Summary

I hope this article will help you understand how to handle the dropdown checkbox and radio buttons using Selenium with Java.

Thanks

Happy learning.


Similar Articles