How to select "Mango" from a Selenium dropdown in Python?

I’m trying to interact with Selenium select dropdown using Python.

First, I need to click on the drop-down menu. Here’s how I attempt to do it: inputElementFruits = driver.find_element_by_xpath(“//select[@id=‘fruits01’]”).click()

After clicking on the drop-down, I want to select a specific option, such as “Mango”. I initially tried to use inputElementFruits.send_keys(…), but it didn’t work as expected.

What’s the correct way to select an option like “Mango” from this drop-down menu using Selenium WebDriver?

Hey Sakshikuchroo,

Here’s how you can interact with dropdown elements using Selenium WebDriver’s Select class in Python:

from selenium.webdriver.support.ui import Select

Selecting dropdown element by its ID

ddelement = Select(driver.find_element_by_id(‘id_of_element’))

Alternatively, select by XPath

ddelement = Select(driver.find_element_by_xpath(‘xpath_of_element’))

Or by CSS selector

ddelement = Select(driver.find_element_by_css_selector(‘css_selector_of_element’))

Selecting ‘Banana’ from the dropdown using different methods:

1. By index (index starts from 0)

ddelement.select_by_index(1)

2. By value attribute

ddelement.select_by_value(‘1’)

3. By visible text displayed in the dropdown

ddelement.select_by_visible_text(‘Banana’)

In this example:

We use Select from selenium.webdriver.support.ui to interact with dropdowns. Replace ‘id_of_element’, ‘xpath_of_element’, or ‘css_selector_of_element’ with the actual locator strings for your dropdown. The select_by_index, select_by_value, and select_by_visible_text methods of Select are used to select options based on their index, value attribute, or visible text, respectively.

This approach ensures precise selection of dropdown options without relying on simulated clicks or manual interactions.

Hey Sakshikuchroo,

If you’re having trouble selecting an option from a dropdown inside a table, you can try simulating key presses to navigate to the desired value. Here’s a Python example using Selenium WebDriver:

Identify the dropdown element by name

elem = browser.find_element_by_name(objectVal)

Loop through each option in the dropdown

for option in elem.find_elements_by_tag_name(‘option’): # Check if the option text matches the desired value if option.text == value: break else: # Simulate pressing the down arrow key ARROW_DOWN = u’\ue015’ elem.send_keys(ARROW_DOWN)

In this code:

browser.find_element_by_name(objectVal) locates the dropdown element by its name. We iterate through each tag inside the dropdown using elem.find_elements_by_tag_name(‘option’). We compare each option’s text (option.text) with the value you want to select. If a match is found, we break out of the loop. If no match is found initially, we simulate pressing the down arrow key (u’\ue015’) using elem.send_keys(ARROW_DOWN) until we find the desired value.

This approach allows you to navigate through the dropdown options and select the one you need, even if it’s embedded within a table structure.