About this lesson
Selenium Python select_by_index: pick a dropdown option by its position number
Selenium Python AssignmentsIntermediateDropdowns
How it works
Clicking a native dropdown's options by hand is clumsy, so Selenium gives you a helper class that does those clicks for you. You import Select from selenium.webdriver.support.ui, wrap the element you found, and call methods on the wrapper. Wrapping any other tag, such as body, raises UnexpectedTagNameException (the message says Select only works on select elements, not on body).
select_by_index counts from zero, in HTML order. A negative number or one past the end raises NoSuchElementException ("Could not locate element with index"). Slicing habits do not carry over: -1 is not the last option here.
Position zero is a trap. This dropdown starts on a disabled placeholder that is already selected, so asking for it does nothing. Once you move to another option, asking for the placeholder again raises NotImplementedError ("You may not select a disabled option").
To see the result, read first_selected_option. It is a property, so you write it without brackets. It gives a normal WebElement, and its text property is a str.
About the assignment
Open the Dropdowns page, choose a language in the single-select list by position instead of by name, and report which label ended up selected.
What you will do
- Find the single select by its id and wrap the element in
Select. - Choose the option at position three. Count the placeholder as position zero.
- Take the wrapper's first selected option and read its
text. - Print the label from the task followed by that text.
Common mistakes
Counting from one
Count the placeholder as one and JavaScript looks like 4, but index 4 is C#, so the wrong language prints, with no error. Pick the wrong number and the wrong language prints, with no error.
Asking for an index past the end
The list has six options, so the last valid number is five. Index 99 raises
NoSuchElementException, and the message names the index.Forgetting the import
Using
Selectwithout importing it givesNameError: name 'Select' is not defined. The starter already imports it, so keep that line.
In a real test suite
Teams seldom select by position, because a new option shifts every number after it. Text or value survives page changes better. Index selection helps when the test wants "any real option" and takes the first one after the placeholder.
A page object often hides this in a method like choose_language. The next lesson reads the whole list of options back, which is how you learn the numbers in the first place.
Related lessons
- Choose a Dropdown Option by Visible TextChoose an option by its visible label, which survives reordering.
- Choose a Dropdown Option by ValueChoose by the value attribute, the third way
Selectoffers. - List Every Dropdown OptionList every option so you can see which number belongs to which label.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/Dropdowns")
# Select index 3 and print the visible text
finally:
driver.quit()