How to select a drop-down menu option value with Selenium (Python)

720    Asked by ananyaPawar in Python , Asked on Apr 14, 2021

I need to select an element from a drop-down menu.

For example:

<select id="fruits01" class="select" name="fruits">

  

  

  

</select>

First I have to click on it. I do this:

inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()

After that I have to select the good element, let us say Mango.

I tried to do it with inputElementFruits.send_keys(...) but it did not work.

Answered by ananya Pawar

In my opinion, unless your click is firing some kind of ajax call to populate your list, you don't actually need to execute the click.

Select the element and enumerate the options, selecting the option(s) you want.

from selenium import webdriver
b = webdriver.Firefox()
b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()

Note: To select a drop-down menu option value with Selenium (Python), firstly you need to import the Select class, and then you need to create the instance of Select class.




You can use selenium select dropdown python as shown below:



Your Answer

Interviews

Parent Categories