Skip to content
CodeKruPro

Select Dropdown by Index with selectByIndex()

28/70

Select Dropdown by Index

selectByIndex() selects an option by its position (0-based index).

On the Dropdowns page, the singleSelect options are:

  • Index 0: "Select Language" (disabled placeholder)
  • Index 1: "Java"
  • Index 2: "Python"
  • Index 3: "JavaScript"
  • Index 4: "C#"
  • Index 5: "Ruby"

Task:

  1. Navigate to https://testkru.com/Elements/Dropdowns
  2. Select the option at index 3 (JavaScript)
  3. Print the selected text

Expected output:

Selected at index 3: JavaScript

About this lesson

selectByIndex in Selenium Java: choose a dropdown option by position

Learn Selenium JavaIntermediateDropdowns

How it works

selectByIndex() chooses the option at a position in the list, counting from 0. The count includes every option, the placeholder too, so on the singleSelect dropdown index 0 is "Select Language" and the first real language is at index 1.

Index 0 here is a disabled option, and Select will not choose it: selectByIndex(0) throws UnsupportedOperationException: You may not select a disabled option. An index past the end throws NoSuchElementException with "Cannot locate option with index".

Position is the least descriptive way to choose. It is handy when the option text is generated or random, for example "pick any available date", but a test that relies on a fixed index breaks as soon as someone adds or reorders an option.

About the assignment

The starter wraps singleSelect in a Select. You choose the option at the index from the task and print the language that sits there, which shows how the placeholder shifts the numbering.

What you will do

  1. Call selectByIndex() with the index from the task.
  2. Get the selected option with getFirstSelectedOption() and read its text.
  3. Print one line with the label from the expected output: it names the index and then the text.

Common mistakes

  • Counting every option from 1

    Counting every option from 1, placeholder included, gives an index one too high, so you land on the option after the one you wanted.

  • Forgetting the placeholder

    The disabled placeholder takes index 0. Counting only the languages from 0 gives an index one too low and picks the option before the one you wanted.

  • Hard-coding the index for a specific option

    If you need a particular option, select it by text or value. Use an index only when any option will do.

In a real test suite

Tests use indexes for "choose any available option" steps. Checking getOptions().size() first makes sure the index exists, and the test does not depend on any label.

Lesson 27 chose by value. Lesson 29 stops choosing and reads the whole option list with getOptions().

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;

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: Select by index 3
        // TODO: Print the selected option text
        
        driver.quit();
    }
}