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
DDDOriginal
2025-01-03 01:30:39628browse

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>

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

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)

Now, you can select options by:

  • Visible Text:

    select.select_by_visible_text('Banana')
  • Value:

    select.select_by_value('1')

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!

Statement:
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