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
- Open the Dropdowns page and locate the single-choice select by its id.
- Wrap that element in
Selectand keep the wrapper in a variable. - Choose the language called Java by passing its visible text to
select_by_visible_text(). - Read the label of the selected option through
first_selected_optionand itstext, then print it after the label.
Common mistakes
Wrong letter case or spacing
The text must match the label exactly. Passing
javaorJavaraisesNoSuchElementException: Could not locate element with visible textand 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_optiongives an element. Joining it to a string raises aTypeErroraboutWebElement; add.textfirst.
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.
Related lessons
- Choose a Dropdown Option by ValueChooses the same dropdown by value, where the word differs from the label.
- Choose a Dropdown Option by IndexChooses by position, the third way
Selectcan match. - Read the Option That Is Already SelectedReads the already selected option, using
first_selected_optionon its own.
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 Java by its visible text and print it
finally:
driver.quit()