Back

Explore Courses Blog Tutorials Interview Questions
+5 votes
1 view
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>

</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.

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()

b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()

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

by (44.3k 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()

driver.get('url')

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

# select by visible text

select.select_by_visible_text('Banana')

# select by value 

select.select_by_value('1')

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.

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")
driver.get("https://test.com")
select = Select(driver.find_element_by_xpath("""//input[@name='n_name']"""))
select.select_by_index(2)

+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 selenium.webdriver.support.select import Select 

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

select_fr.select_by_index(0)

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

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...