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
- Open the Dropdowns page, locate the single-choice select by its id, and wrap it in
Select. - Call
select_by_value()with the value word given in the task, written exactly as the attribute holds it. - Read the selected option through
first_selected_option. - 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_valuewith the capitalised label raisesNoSuchElementException: Cannot locate option with value: Python. The attribute is lowercase, so write it that way.Printing the value instead of the label
Reading
get_attributeforvalueon the selected option gives the lowercase word. The task wants what the user sees, which is.textof the option.Choosing a value that does not exist
A made-up value such as
cobolraisesNoSuchElementException: 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.
Related lessons
- Choose a Dropdown Option by Visible TextChooses by visible label, the sibling method to this one.
- Choose a Dropdown Option by IndexChooses by index when neither label nor value is a good handle.
- List Every Dropdown OptionLists every option, which is how you discover the real values.
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 value python and print the visible text
finally:
driver.quit()