Home > Backend Development > Python Tutorial > How to Select Drop-Down Menu Values Using Selenium in Python?

How to Select Drop-Down Menu Values Using Selenium in Python?

DDD
Release: 2025-01-03 01:30:39
Original
648 people have browsed it

How to Select Drop-Down Menu Values Using Selenium in Python?

Selecting a Drop-Down Menu Value with Selenium in Python

When interacting with web forms, selecting values from drop-down menus is a common task. Selenium provides robust mechanisms to accomplish this action.

For instance, consider a drop-down menu with the following HTML structure:

<select>
Copy after login

To select an option, follow these steps:

1. Click on the Drop-Down Menu:

inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']")
inputElementFruits.click()
Copy after login

2. Utilize the Select Class:

Selenium's Select class simplifies the selection process. To use it:

import selenium.webdriver.support.ui as Select

Then, instantiate the Select class with the drop-down element:

select = Select(inputElementFruits)
Copy after login

Now, you can select options by:

  • Visible Text:

    select.select_by_visible_text('Banana')
    Copy after login
  • Value:

    select.select_by_value('1')
    Copy after login

Additional Resources:

  • [Official Selenium Documentation](https://www.selenium.dev/documentation/webdriver/elements/select/)
  • [Selecting from HTML Drop-Down Menu using Selenium and Python](https://www.edureka.co/blog/select-html-drop-down-selenium/)

The above is the detailed content of How to Select Drop-Down Menu Values Using Selenium in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template