Skip to content
CodeKruPro

Get All Dropdown Options with getOptions()

29/70

Get All Dropdown Options

getOptions() returns a list of all <option> elements in the dropdown.

Task:

  1. Navigate to https://testkru.com/Elements/Dropdowns
  2. Get all options from the singleSelect dropdown
  3. Print the total count
  4. Print each option's visible text (skip the placeholder)

Expected output:

Total options: 6
Java
Python
JavaScript
C#
Ruby

About this lesson

Get all dropdown options in Selenium Java with getOptions()

Learn Selenium JavaIntermediateDropdowns

How it works

getOptions() returns every option inside the dropdown as a List<WebElement>, in page order, whether or not it is selected or disabled. It is how a test checks what a dropdown offers rather than what it has chosen.

The list includes the placeholder. On the singleSelect dropdown that is the disabled "Select Language" option at position 0, so the real choices start at position 1. For a dropdown with optgroup sections, like groupedSelect on the same page, getOptions() returns the options of every group in one flat list, and the group labels are not in it.

To get text you call getText() on each option. Calling getText() on the select itself gives one long string with every option on its own line, padded with the whitespace from the HTML, which is hard to compare.

About the assignment

The starter wraps singleSelect in a Select. You read its full option list, print how many options it has, then print each language on its own line, leaving the placeholder out of the printed names.

What you will do

  1. Call getOptions() and keep the list.
  2. Print its size() after the label Total options:. The count includes the placeholder.
  3. Loop over the list starting at position 1, so the placeholder is skipped, and print each option's getText() on its own line with no label.

Common mistakes

  • Skipping the placeholder in the count

    The count line uses the full list. Removing the placeholder before calling size() prints a number one lower than expected.

  • Printing the placeholder

    A plain for-each loop prints "Select Language" first. Start the loop at 1, or filter out options whose isEnabled() is false.

  • Using getAllSelectedOptions()

    That only returns options that are currently selected, which here is just the placeholder.

In a real test suite

Option lists are checked against requirements: every supported country is present, the list is sorted, nothing is duplicated. Mapping each option to its getText() in a stream gives a List<String> you can compare in one assertion.

Lesson 28 chose by position. Lesson 30 turns to a dropdown that allows more than one choice at once.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Dropdowns");
        
        Select dropdown = new Select(driver.findElement(By.id("singleSelect")));
        
        // TODO: Get all options and print count
        // TODO: Print each option text (skip the first placeholder option)
        
        driver.quit();
    }
}