Skip to content
CodeKruPro

Drive a Dropdown from a List of Choices

79/80

Drive a Dropdown from a List of Choices

A list can drive the same steps more than once. Print each result immediately so the lines follow the list order.

Task:

  1. Loop over ["Python", "Ruby"]
  2. Select each choice in #singleSelect
  3. Print #singleSelectResult before the next choice

Print the lines below exactly.

Expected output:

Selected: Python
Selected: Ruby

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

  1. In the loop body, remove the pass placeholder.
  2. Choose the option whose visible text equals the loop variable.
  3. Find the result element on the page and read its text.
  4. 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 p tag. Wrapping it raises UnexpectedTagNameException: 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.

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.

main.py
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()