DEV Community

Cover image for Easiest Way to Handle Drop Down Menus in Python Using Selenium ?
Ali
Ali

Posted on

1

Easiest Way to Handle Drop Down Menus in Python Using Selenium ?

Introduction

Selenium has a neat way to handle drown down menus by using the Select function.

For this example, we will testing it out on:
https://app.endtest.io/guides/docs/how-to-test-dropdowns/

Drop down test

Importing the Select class

First let’s import the Select function.

from selenium.webdriver.support.select import Select

Finding the Drop Down Element

Now let's call the drop down by using its ID, which is pets and name its instance drop_down.

drop_down = driver.find_element_by_id('pets')

Selecting the drop down

Now we've got the drop down selected and will name its instance drop.

drop = Select(drop_down)

There are multiple ways we can select values in a drop down menu, either by index, value or visible text.

Selecting by Index

drop.select_by_index(2)

Selecting by Value

drop.select_by_value('cat')

Selecting by Visible Text

drop.select_by_visible_text("Dog")

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →