How to Select a Drop-Down Menu Value with Python Selenium

Rate this post

One of the finest open-source browser automation tools recognized in today’s world is selenium and the “Select” class of selenium WebDriver enables us to handle the dropdown menu smoothly. Today, we will try to figure out all the intricacies of the “Select” class practically.

Purpose of the Select Class

The “Select” class in selenium allows us to toggle the state of the drop-down menu that is created with the <select> html tags. It is possible to instantiate an object from this class and apply various methods on this object no matter it is a single-select or multi-select dropdown menu. Both select and deselect methods are available for the Select class object while deselect method is only applicable for the <multi-select> HTML tags where multiple selections of values are possible.

Instantiating an Object of the Select Class

To start the process we need to import WebDriver from the selenium package. We will use ChromeDriver which allows us to navigate through the webpage using a chrome browser. There is a special Select module available in the selenium package which  will be imported from selenium.webdriver.support.ui:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome(executable_path = r'G:/scraping_practice/chromedriver_win32/chromedriver.exe')
driver.get('https://www.imdb.com/search/title/')

Chromedriver path is specified and the driver is using the get method to connect to the “IMDB advanced title search” site.

Our goal is to automate the user rating dropdown menu and set the minimum rating to 7 because I love to watch a highly-rated movie. By right-clicking the user rating dropdown menu we can inspect the HTML code.

Image:1

                                                              

From the HTML code, we can see the <select> HTML tag is used to control the drop-down “user rating” menu, we can select this tag by “name” attribute and create a WebElement “rating” and then click on it.

rating = driver.find_element_by_name('user_rating-min')
rating.click()

Now we can easily instantiate an object of Select class by using a new keyword. After creating an object, we need to pass a dropdown WebElement as a parameter to its constructor. We have created a variable rating of type WebElement and now we will pass it as a parameter in the “Select” class to create a rating_dropdown object.

rating_dropdown = Select(rating)

Remember: The Select class starts with the alphabet capital “S” and the select() method starts with the alphabet small “s”

Selecting a Value from the Dropdown

The Select class provides us with multiple methods to select a dropdown value. Let’s get acquainted with those:

select_by_visible_text()

From the HTML code (Image:1) we can select a value from <option> tag  with the select() method by text. we just need to put the desired text in string format inside the parenthesis. Let’s try to set the user rating to 7.0:

rating_dropdown.select_by_visible_text('7.0')

if we run the script now we will see our user rating is automatically set to 7.0 by the selenium web driver.

select_by_index()

We want to load 100 results per page for each advanced search. How can we do that? At the last portion of the page, there is a display options menu. Let’s try to set the dropdown menu to 100 per page. Inspect the HTML tag and find the select tag.

Image:2

As we can see 100 per page is the 2nd element of <select> tag we can use indexing to reach there.

display = driver.find_element_by_id('search-count')
display.click()
display_dropdown = Select(display)
display_dropdown.select_by_index('1')

select_by_value()

Now, we will choose “English” from the languages drop-down.

Image:3

As we inspect HTML we can see the “value” element is available inside <option> tag. First we need to find the <select> tag and then enter the value that represents English language:

language = driver.find_element_by_name('languages')
language_dropdown = Select(language)
language_dropdown.select_by_value('en')

Selecting Multiple Values from the Dropdown

From Image:3 we can see a “multipleattribute is available inside the <select> tag. This “multiple” attribute allows us to select multiple values from the languages dropdown.

Now lets enhance our language choice with ‘German’ , ‘French’ and ‘Afrikaans’ :

language_dropdown.select_by_value('de')
language_dropdown.select_by_value('fr')
language_dropdown.select_by_value('af')

If we run the code we will see ‘German’ and ‘French’ are also selected with English.

How to Deselect a Value from Multiple Selected Values

Just like the select method we can use deselect method to deselect the value from multiple selected values. This option is only available for the multi-select drop-down. We can deselect any pre-selected value from the multi-select dropdown by using various methods discussed below:

deselect_by_index():

Any pre-selected value can be deselected with deselect_by_index() method. This works, in the same way, the select_by_index() method works. We just need to select the index of the value from the select tag. Let’s try to deselect “Afrikaans” language from the language dropdown. as the index of the ‘Afrikaans’ language is 4, the code should be:

Image:4
language_dropdown.deselect_by_index('4')

The ‘Afrikanns’ language will be deselected.

deselect_by_value():

We can do the same thing to deselect the selected element by the “value” attribute. as we can see the value attribute of “Afrikaans” language is "af", the code should be:

language_dropdown.deselect_by_value('af')

deselect_by_visible_text():

As for the select_by_visible_text method, we can deselect a pre-selected value from multiple dropdowns with a text element. If the ‘Afrikaans’ language is already selected and we want to deselect it, we can do it like this:

language_dropdown.deselect_by_visible_text('Afrikaans')

deselect_all()

Sometimes we need to clear all the values that have been selected. In this case, we may use the deselect_all() option. This method only works where “multiple” select option is available. Let’s clear all the selected languages from the languages dropdown and make it fresh.

language_dropdown.deselect_all()

all the selected items will be deselected.

How to Use Options

If we want to know how many options are available in a certain dropdown, the “options” method will allow us versatile functionality. Let’s try to count how many options are available for selection in the ‘languages’ dropdown:

language_option = language_dropdown.options
print(len(language_option))

Here, the ‘language_option’ variable is treated as a list type and we can use the ‘len’ function to get the total number of languages that can be selected. The result will be 334 languages in total.

We can use list indexing to select any language with options method. let’s try to select the first language from the languages dropdown:

language_option[0].click()

Now we want to select all the languages. How can we do that? We can use a for loop as a solution.

for language in language_option:
    language.click()

If we run the code, all languages will be selected.

Conclusion

That’s all about “Select” class for today. Hope you enjoyed it. Feel free to hover your mouse to this link if you want to learn more and check out our free email academy with Python cheat sheets and free Python courses: