DEV Community

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

Posted on

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")

Top comments (0)