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
- Call
selectByIndex()with the index from the task. - Get the selected option with
getFirstSelectedOption()and read its text. - 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().
Related lessons
- Get All Dropdown Options with getOptions()Get the full list of options and their positions.
- Select Dropdown Option by Visible TextChoose by visible text, the most readable option.
- Find Multiple Elements with findElements()Lists from Selenium are zero-based Java lists, as
findElements()showed.
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.
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();
}
}