Skip to content
CodeKruPro

Choose a dropdown option by index

Automation taskEasyAsked in: Accenture, Capgemini, Infosys, SaaS Labs, TCS, Wipro

A follow-up: "Does the index start at zero, and is it a good idea?" It counts from 0, including a placeholder option such as "Choose one", and it points at a different option as soon as options are added or removed.

Select Dropdown by Index with selectByIndex()

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