About this lesson
Get all dropdown options in Selenium Python: loop over Select.options and skip the placeholder
Selenium Python AssignmentsIntermediateDropdowns
How it works
A Select wrapper has an options property. It returns a plain Python list of WebElement objects, one per option, in page order. Everything you know about lists applies: len() gives the count, a for loop walks it, and a slice cuts pieces from it.
options is a property, not a method, so select.options() fails with a TypeError because a list cannot be called. Each item is an element. You read its label with .text and its value with get_attribute("value"). The placeholder is disabled and has an empty value.
The count includes the placeholder, which is why the page has six options but only five languages. A slice that starts at 1 drops the first item and keeps the rest, so the loop needs no counter.
len() returns an int, and adding an int to a string raises TypeError with the message can only concatenate str (not "int") to str. Wrap the count in str() first.
About the assignment
Report how many options the single-select dropdown holds, then list the real language labels, leaving the "Select Language" prompt out.
What you will do
- Wrap the single select with
Selectand read itsoptions. - Print the count label from the task and the length of the list, converted to text.
- Loop over the options, starting after the placeholder, and print the
textof each on its own line. - Keep the count line first, then the five labels in page order.
Common mistakes
Calling options like a function
select.options()raisesTypeError, because a list is not callable. Drop the brackets.Adding the count without str()
Putting
len(...)after a string and a plus sign raisesTypeError. Python will not join a string and an int. Convert the number first.Printing the placeholder as a language
Looping over the whole list shows "Select Language" as the first line. It is a real option, just not a real choice. Slice from position one.
In a real test suite
In a suite this shows up as a test that the dropdown offers the expected labels. A list comprehension turns the options into a list of strings that you compare to an expected list in one assertion.
The same list can feed parametrize, so a test runs once per label. The next lesson moves to a dropdown where more than one option can be chosen at once.
Related lessons
- Choose a Dropdown Option by IndexPick one option by position, using the numbering you see in this list.
- Select and Deselect in a Multi-SelectWork with a select that accepts several chosen options at once.
- Find Every Radio ButtonAnother lesson built on a Python list returned by
find_elements.
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 count, then each language label
finally:
driver.quit()