Home >Backend Development >Python Tutorial >How to Select a Drop-Down Menu Value with Selenium in Python?

How to Select a Drop-Down Menu Value with Selenium in Python?

DDD
DDDOriginal
2024-12-07 10:33:15218browse

How to Select a Drop-Down Menu Value with Selenium in Python?

Selecting a Drop-Down Menu Value with Selenium Using Python

You have a drop-down menu and the element you need to select has an id equal to 'fruits01'.

  1. Click on it.
inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()
  1. Select the desired element.

You attempted to use inputElementFruits.send_keys(...), but this approach won't work. Instead, utilize the Selenium Select class specifically designed for handling drop-down menu elements.

import selenium.webdriver.support.ui as select

selectElement = Select(inputElementFruits)
selectElement.select_by_visible_text('Mango') # choose by visible text

Alternatively, you can select by value:

selectElement.select_by_value('2') # select by value ('2' corresponds to Mango)

References:

[Appropriate method to select an option from a drop-down list using Python's WebDriver](https://stackoverflow.com/questions/45897309/correct-way-to-select-an-option-from-a-dropdown-list-using-seleniums-python-webdriv)

The above is the detailed content of How to Select a Drop-Down Menu Value with 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