In HTML, dropdowns are generally implemented using either the <select> tag or the <input> tag.

For dropdowns created using the <select> tag, Selenium WebDriver provides the Select class from the org.openqa.selenium.support.ui package.

Create a Select object by passing the dropdown WebElement to its constructor.

Advertisement
 
Select se = new Select(driver.findElement(By.xpath("//*[@id='oldSelectMenu']")));
 

The Select class provides various methods to perform dropdown operations.


Selecting a Value from a Dropdown

1. selectByIndex()

Selects an option using its index number.

  • Index starts from 0.
 
Select se = new Select(driver.findElement(By.xpath("//*[@id='oldSelectMenu']")));

se.selectByIndex(3);
 

2. selectByValue()

Selects an option using the value attribute of the option.

 
se.selectByValue("6");
 

3. selectByVisibleText()

Selects an option using the text visible to the user.

Works for both:

  • Single-select dropdowns
  • Multi-select dropdowns
 
se.selectByVisibleText("Volvo");
 

Multi-Select Dropdowns

If a <select> tag contains the multiple attribute, multiple options can be selected.

Example

 
<select multiple>
 

Checking Whether a Dropdown Supports Multiple Selection

Use the isMultiple() method.

It returns:

  • true
  • false
 
if (oSel.isMultiple()) {

    oSel.selectByValue("volvo");
    oSel.selectByValue("audi");

    oSel.selectByVisibleText("Volvo");
    oSel.selectByVisibleText("Opel");

}
 

Checking Whether a Checkbox Is Displayed

Use the isDisplayed() method.

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

if (checkbox.isDisplayed()) {

    System.out.println("Checkbox is displayed.");

} else {

    System.out.println("Checkbox is not displayed.");

}
 

Printing a Message When an Element Is Not Present

If an element is unavailable, findElement() throws NoSuchElementException.

Handle it using a try-catch block.

 
try {

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

    // Perform actions

} catch (NoSuchElementException e) {

    System.out.println("Element is not present on the page.");

}
 

Checking All Checkboxes on a Page

Step 1: Locate All Checkboxes

 
List<WebElement> checkboxes =
driver.findElements(By.cssSelector("input[type='checkbox']"));
 

Step 2: Loop Through Each Checkbox

 
for (WebElement checkbox : checkboxes) {

    checkbox.click();

}
 

This selects every checkbox on the page.

If checkboxes share a common class name, you can also use:

 
By.className("checkbox-class")
 

To avoid unchecking already selected checkboxes:

 
if (!checkbox.isSelected()) {

    checkbox.click();

}
 

Verifying Whether a Checkbox or Radio Button Is Selected

Use the isSelected() method.

It returns:

  • true → Selected
  • false → Not Selected

Checkbox

 
WebElement checkbox =
driver.findElement(By.id("example-checkbox"));

boolean isChecked = checkbox.isSelected();

System.out.println("Checkbox Checked: " + isChecked);
 

Radio Button

Exactly the same approach.

 
WebElement radio =
driver.findElement(By.id("male"));

System.out.println(radio.isSelected());
 

Differentiating Multiple Checkboxes

When several checkboxes exist, identify them using one of the following approaches.

1. Unique ID

 
WebElement checkbox1 =
driver.findElement(By.id("checkbox1"));
 

2. List and Index

 
List<WebElement> all =
driver.findElements(By.cssSelector("input[type='checkbox']"));

WebElement thirdCheckbox = all.get(2);
 

3. Associated Label

Locate the checkbox using its nearby label or surrounding element.

4. Visible Text

Locate the checkbox based on the label text associated with it.


Text Box Operations

Clearing Text

Use clear().

 
textboxElement.clear();
 

Reading Text from a Text Box

The entered text is stored inside the value attribute.

 
String typedText =
textboxElement.getAttribute("value");
 

Sending ENTER or TAB Keys

Use the Keys class.

ENTER

 
searchInput.sendKeys("Selenium WebDriver" + Keys.ENTER);
 

TAB

 
searchInput.sendKeys(Keys.TAB);
 

Typing Multiple Lines in a Text Area

Use Keys.ENTER between lines.

 
WebElement textarea =
driver.findElement(By.id("textarea-id"));

textarea.sendKeys("First line");

textarea.sendKeys(Keys.ENTER);

textarea.sendKeys("Second line");
 

FAQs

1. How do you handle dropdowns in Selenium?

For dropdowns created using the <select> tag, create a Select object and use its methods.

The Select class belongs to:

 
org.openqa.selenium.support.ui.Select
 

2. What are the three methods used to select a dropdown value?

  • selectByIndex(int)
  • selectByValue(String)
  • selectByVisibleText(String)

3. How do you check whether a dropdown supports multiple selection?

Use:

 
isMultiple()
 

It returns:

  • true
  • false

4. How do you verify whether a checkbox or radio button is selected?

Use:

 
isSelected()
 

Returns:

  • true → Selected
  • false → Not Selected

5. How do you select all checkboxes on a page?

Locate all checkboxes.

 
findElements(By.cssSelector("input[type='checkbox']"))
 

Loop through the list and click each checkbox.

 
for(WebElement checkbox : checkboxes){

    checkbox.click();

}
 

6. How do you read the text entered into a textbox?

Use:

 
getAttribute("value")
 

Example:

 
String text =
textbox.getAttribute("value");
 

7. How do you send ENTER or TAB keys?

Use the Keys class.

Example:

 
element.sendKeys("Selenium" + Keys.ENTER);
 

or

 
element.sendKeys(Keys.TAB);
 

8. How do you type text on a new line inside a textarea?

Use Keys.ENTER.

 
textarea.sendKeys("First Line");

textarea.sendKeys(Keys.ENTER);

textarea.sendKeys("Second Line");