+5 votes
1 view
in DevOps and Agile by (20k points)

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

For example:

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

  <option value="0">Choose your fruits:</option>

  <option value="1">Banana</option>

  <option value="2">Mango</option>


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.

5 Answers

+4 votes
by (28.4k points)
edited by

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. For example,

from selenium import webdriver

b = webdriver.Firefox()


For more information please go through the following tutorial to get more info about Selenium:

To learn in-depth about Selenium, sign up for an industry based Selenium training

by (44.6k points)
using Select class makes the problem much easier to solve, check out the below answer.
by (40.4k points)
It worked for me. Thank you.
+4 votes
by (108k points)

Selenium presents a convenient Select class to work with select -> option constructs:

from selenium import webdriver

from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()


select = Select(driver.find_element_by_id('fruits01'))

# select by visible text


# select by value 


by (19.8k points)
Thanks, by importing the library for select class and using its in-built method select_by_visible_text() helped me.
by (19.9k points)
This was very easy to understand. Thank you.
by (33.2k points)
Thanks, it solved my problem.
by (47.2k points)
This is a great way to go and should be the de facto method.
+1 vote
by (30k points)

For this, you can use a css selector 

driver.find_element_by_css_selector("#fruits01 [value='1']").click()

Change the 1 in the attribute = value css selector to the value corresponding with the desired one.

by (32.1k points)
I believe if you use select class, it would be a better way to solve this problem.
+1 vote
by (29.8k points)
Try using the following:

from selenium.webdriver.support.ui import Select
driver = webdriver.Ie(".\\IEDriverServer.exe")
select = Select(driver.find_element_by_xpath("""//input[@name='n_name']"""))


+1 vote
by (107k points)

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. After completing these two steps you will need to create the instance of Select class, you can perform select methods on that instance to select an option from the dropdown list. 

See the code for the same below:-

from selenium.webdriver.support.select import Select 

select_fr = Select(driver.find_element_by_id("fruits01")) 


Welcome to Intellipaat Community. Get your technical queries answered by top developers !