Home >Backend Development >Python Tutorial >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'.
inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()
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!