Skip to content
CodeKruPro

List Every Dropdown Option

32/80

List Every Dropdown Option

options is every option element, including the placeholder. Print the count, then skip the placeholder when you print labels.

Task:

  1. Print how many options #singleSelect has
  2. Print each label except the placeholder

Print the lines below exactly.

Expected output:

Total options: 6
Java
Python
JavaScript
C#
Ruby

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

  1. Wrap the single select with Select and read its options.
  2. Print the count label from the task and the length of the list, converted to text.
  3. Loop over the options, starting after the placeholder, and print the text of each on its own line.
  4. Keep the count line first, then the five labels in page order.

Common mistakes

  • Calling options like a function

    select.options() raises TypeError, because a list is not callable. Drop the brackets.

  • Adding the count without str()

    Putting len(...) after a string and a plus sign raises TypeError. 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.

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 count, then each language label
finally:
    driver.quit()