About this lesson
Selenium Python first_selected_option: get the selected dropdown text and its value
Selenium Python AssignmentsIntermediateDropdowns
How it works
Not every test starts by choosing something. Sometimes the page loads with a choice already made, and the test must confirm it. On a Select wrapper, first_selected_option returns the element for the first selected option. In a single-select that is the only choice.
It is a property, so write it without brackets. Add them and Python raises TypeError: 'WebElement' object is not callable, because the element was already returned before the brackets were reached.
The result is an ordinary WebElement. Its text property gives the label the user sees. The value is a different thing, and element.value does not exist in Python: it raises AttributeError: 'WebElement' object has no attribute 'value'. Read it with get_attribute("value"), which returns a str. On this dropdown the label is JavaScript and the value is its lower-case form, so the two differ.
A test about what the user sees checks the label. A test about what the form submits checks the value.
About the assignment
The pre-selected dropdown opens with one language already chosen. Read that option and report its visible text and its value.
What you will do
- Find the dropdown that already has a selection and wrap it with
Select. - Take its
first_selected_optionand keep the element in a variable, so you can ask it two questions. - Print the label text with the text property, then the value with
get_attribute. - Start each line with its own label from the task, with strings on both sides of the plus sign.
Common mistakes
Looking at the wrong dropdown
The single-select on the same page starts on its placeholder, so reading it gives "Select Language". The pre-selected one has a different id.
Using .value on the element
A Python
WebElementhas novalueattribute and raisesAttributeError. Useget_attribute("value")orget_property("value").Expecting a list
Some learners loop over
first_selected_optionas if it wereall_selected_options. The first is one element. The second is a list.
In a real test suite
Checking defaults is a common regression test: a country picker should start on the right country, a sort menu on "Newest". Teams wrap it in a small function that returns the selected label.
Assertions on both text and value matter when the two can drift apart, for example after a translation change. The next lessons move on to element states such as visible, enabled and selected.
Related lessons
- Choose a Dropdown Option by IndexSelect an option by position, then read it back with the same property.
- Read an AttributeRead an attribute from an element, the method used here for the value.
- Select and Deselect in a Multi-SelectSee how
all_selected_optionsdiffers when several options are selected.
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")
# Print the text and value of the option already selected in #preSelectedDropdown
finally:
driver.quit()