Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+5 votes
in DevOps and Agile by (19.4k 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 (27.5k 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

Wanna become an Expert in python? Come & join our Python Certification course

How to select a drop-down menu option value with Selenium (Python)
by (44.4k points)
using Select class makes the problem much easier to solve, check out the below answer.
by (40.7k 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 import Select

driver = webdriver.Firefox()


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

# select by visible text


# select by value 


How to select a drop-down menu option value with Selenium (Python)
by (19.7k 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.1k 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 (29.3k 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.

How to select a drop-down menu option value with Selenium (Python)
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.5k points)

Try using the following:

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

+1 vote
by (106k 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 import Select 

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


Browse Categories