Home >Backend Development >Python Tutorial >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:
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!