Skip to content
CodeKruPro

Choose a Dropdown Option by Visible Text

29/80

Choose a Dropdown Option by Visible Text

Wrap the select element in Select. select_by_visible_text chooses the option whose label you can see.

Task:

  1. Open Dropdowns
  2. Select Java in #singleSelect
  3. Print the selected option text

Print the lines below exactly.

Expected output:

Selected: Java

About this lesson

Pick a dropdown option with Select and select_by_visible_text in Selenium Python

Selenium Python AssignmentsIntermediateDropdowns

How it works

A dropdown built with the select tag has its own helper class in Python: Select, imported from selenium.webdriver.support.ui. You pass it the located element, and it gives you methods for choosing options. The starter already has the import. Passing a different tag, such as a heading, raises UnexpectedTagNameException: Select only works on <select> elements, not on h1.

select_by_visible_text() picks the option whose label matches the text you give. The match is exact. A different letter case, such as java for Java, or extra spaces around the word, raises NoSuchElementException: Could not locate element with visible text.

To read the result use first_selected_option. It is a property, so no brackets, and it returns an element, not a string. Add .text to get the label. Joining the element straight to a label raises TypeError with the message can only concatenate str (not "WebElement") to str. Keep the Select object in a variable, because both the choosing and the reading go through it.

About the assignment

Choose one language in the single-choice dropdown by its label, and then confirm by reading back which option the browser now shows as selected.

What you will do

  1. Open the Dropdowns page and locate the single-choice select by its id.
  2. Wrap that element in Select and keep the wrapper in a variable.
  3. Choose the language called Java by passing its visible text to select_by_visible_text().
  4. Read the label of the selected option through first_selected_option and its text, then print it after the label.

Common mistakes

  • Wrong letter case or spacing

    The text must match the label exactly. Passing java or Java raises NoSuchElementException: Could not locate element with visible text and echoes what you passed.

  • Calling first_selected_option with brackets

    It is a property. Adding brackets raises TypeError: 'WebElement' object is not callable.

  • Printing the element instead of its text

    first_selected_option gives an element. Joining it to a string raises a TypeError about WebElement; add .text first.

In a real test suite

Test suites keep the choices in data, often a list given to pytest.mark.parametrize, and a page object method such as choose_language(label) wraps Select. The label approach is the most readable because the same words appear on screen.

The next lesson chooses by the value attribute, which can differ from the 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.

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")
    # Select Java by its visible text and print it
finally:
    driver.quit()