Home > Backend Development > Python Tutorial > Easiest Way to Handle Drop Down Menus in Python Using Selenium ?

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

Susan Sarandon
Release: 2024-11-03 06:34:02
Original
832 people have browsed it

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/

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

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

The above is the detailed content of Easiest Way to Handle Drop Down Menus in Python Using Selenium ?. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template