Skip to content
CodeKruPro

Choose a Dropdown Option by Value

30/80

Choose a Dropdown Option by Value

select_by_value matches the option value attribute, which can differ from the label.

Task:

  1. Select the option whose value is python
  2. Print the visible text of the selected option

Print the lines below exactly.

Expected output:

Selected: Python

About this lesson

select_by_value in Selenium Python: pick a dropdown option when its label and value attribute differ

Selenium Python AssignmentsIntermediateDropdowns

How it works

Each option inside a select has two pieces of data: the label a user reads and a value attribute the page sends when a form is submitted. They often look similar and still differ. On this practice page the label is Python with a capital letter, while the value is all lowercase. select_by_value() looks only at the attribute.

That is why mixing them up fails. Passing the label word to select_by_value() raises NoSuchElementException: Cannot locate option with value: Python, because no option has that capital-letter value. The mirror mistake with select_by_visible_text() fails the same way.

After choosing, you usually want to know the choice took effect. first_selected_option returns the selected option as an element. Its .text is the label, and get_attribute() with the name value gives the attribute. The task wants the label, which can differ from what you passed in. Wrap the select element in Select once and reuse the variable for the choosing and the reading.

About the assignment

Pick a language by its hidden value instead of its visible label, then print the label that the dropdown now shows. You are checking that the value-to-label mapping is what you think it is.

What you will do

  1. Open the Dropdowns page, locate the single-choice select by its id, and wrap it in Select.
  2. Call select_by_value() with the value word given in the task, written exactly as the attribute holds it.
  3. Read the selected option through first_selected_option.
  4. Print the visible label of that option, not the value, after the label text.

Common mistakes

  • Passing the label to select_by_value

    The call select_by_value with the capitalised label raises NoSuchElementException: Cannot locate option with value: Python. The attribute is lowercase, so write it that way.

  • Printing the value instead of the label

    Reading get_attribute for value on the selected option gives the lowercase word. The task wants what the user sees, which is .text of the option.

  • Choosing a value that does not exist

    A made-up value such as cobol raises NoSuchElementException: Cannot locate option with value: cobol. Look at the option list on the page, or in DevTools, before you guess.

In a real test suite

Teams prefer select_by_value() when labels change often, because the value is stable and tied to the code behind the form. Test data then holds the value and the expected label side by side, and a parametrized test checks both.

The next lesson picks an option by its position in the list.

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 value python and print the visible text
finally:
    driver.quit()