Skip to content
CodeKruPro

Read the Option That Is Already Selected

34/80

Read the Option That Is Already Selected

first_selected_option is the option that is selected when the page loads. On #preSelectedDropdown that option is JavaScript, and its value is javascript.

Task:

  1. Read the already selected option
  2. Print its visible text and its value

Print the lines below exactly.

Expected output:

Selected text: JavaScript
Selected value: javascript

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

  1. Find the dropdown that already has a selection and wrap it with Select.
  2. Take its first_selected_option and keep the element in a variable, so you can ask it two questions.
  3. Print the label text with the text property, then the value with get_attribute.
  4. 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 WebElement has no value attribute and raises AttributeError. Use get_attribute("value") or get_property("value").

  • Expecting a list

    Some learners loop over first_selected_option as if it were all_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.

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")
    # Print the text and value of the option already selected in #preSelectedDropdown
finally:
    driver.quit()