About this lesson
Selenium Python: loop over a list and use select_by_visible_text on a dropdown
Selenium Python AssignmentsAdvancedStructure
How it works
A Select object wraps a real select element. Its method select_by_visible_text picks the option whose shown label matches the string you pass. Wrapping a different tag, such as a p, raises UnexpectedTagNameException, so the element you hand over must be a true dropdown.
Pair that with a Python list and a for loop to repeat the same step with different data. The loop variable holds one label per pass, and you give it to the Select method. This is the idea behind data-driven tests: the steps are written once and the list changes. The one rule is to keep the read inside the loop. Printing afterwards shows only the final state.
The label must match exactly, including capitals and spaces. A lowercase spelling of an option, or an extra space at the start, raises NoSuchElementException with a message beginning Could not locate element with visible text. Passing a list instead of a string fails the same way. After a choice, the page updates a result element. Read its text inside each pass, since text asks the browser again and returns the current value.
About the assignment
The starter creates the Select object and the loop, with a placeholder body. For each language in the list, choose it in the single-select dropdown, then print what the result element says before the next pass begins.
What you will do
- In the loop body, remove the
passplaceholder. - Choose the option whose visible text equals the loop variable.
- Find the result element on the page and read its
text. - Print that text straight away, so the lines come out in the same order as the list.
Common mistakes
Misspelling the option text
A lowercase or padded label raises
NoSuchElementException: Could not locate element with visible text. Copy the label as it appears.Printing after the loop
Moving the print out of the loop shows only the last choice, because the result element keeps one value at a time.
Wrapping the result element in Select
The result is a
ptag. Wrapping it raisesUnexpectedTagNameException: Select only works on <select> elements, not on p. Only the dropdown gets wrapped.
In a real test suite
In pytest this pattern becomes parametrize: one test function, a list of inputs, and one reported result per input. Failures then name the exact value that broke.
The final lesson of the track joins a wait, a frame and an alert into one script.
Related lessons
- Choose a Dropdown Option by Visible TextPicks one option by visible text, the single step this loop repeats.
- List Every Dropdown OptionLists every option of a dropdown, useful for building the list of choices.
- Read the Option That Is Already SelectedReads the option that is already selected, an alternative to the result element.
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")
dropdown = Select(driver.find_element(By.ID, "singleSelect"))
for choice in ["Python", "Ruby"]:
# Select the choice and print #singleSelectResult
pass
finally:
driver.quit()