Dropdowns are an inherent part of any web form that is out there on internet. If you have to select from a list of available options, then the best way to implement that is a dropdown. A very common example would be selected of the year of birth, ir you day or the month. You would have seen a scrollable dropdown which allows you selection of a single element out from a list of various other elements.
A dropdown is not a single element per-se. It actually is an assembly or a class of elements. For example you can say that the country is the class, and the values available in dropdown are it’s various smaller objects.
To work with a dropdown, first you need to select or find the main element group and then go inside further and select the sub-element that you want to select for.
Selecting A DropDown
Since a dropdown has many options, so it is important that we select something separately. Selenium Python API provides the
Select class, which allows you to select the element of your choice.
You can read about in detail here.
Note : The
Select class only works with tags which have select tags.
- Using the Index of Dropdown
- Using the Value of Dropdown
- Using the Text of Dropdown
Using Index of Dropdown :
If the dropdown has an “index” attribute, then we can use that index to select a particular option. You need to be careful while using this way, because it is not un-common to have the index start at 0, and so if you are thinking that index works similarly as country, then you might be mistaken.
We can use the
select_by_index( ) method to select an option using the
s1= Select(driver.find_element_by_id('id_of_element')) s1.select_by_index(5)
Using Value of Dropdown :
If the html mark-up defines an option tag, then you can use the value matching the argument. Suppose the html for dropdown is like this
We can use the
select_by_value( ) method to select an option using the
s2= Select(driver.find_element_by_id('id_of_element')) s2.select_by_value('foo')
Using Text of Dropdown :
Probably the easiest way of doing it. You have to match the text which is displayed in the drop down.
s3= Select(driver.find_element_by_id('id_of_element')) s3.select_by_visible_text('element_text')
Now, how about removing our selections.
De-Selecting Options From Dropdown
Now, what if you want to de-select the option that you have just select? You can achieve this too, using the following methods:
Clear all the selected options. This is only applicable in case of multiple selections. If you have a multiple selection box, then you can use this to de-select any selected option.
If you try to use this in case of single selection, this will throw a
Clear the selected option using the “index” attribute. This is opposite of the
select_by_index( ) method.
Clear the selected option using the value of the option. This is the opposite of the
select_by_value( ) method
Clear the selected option using the text of the option. This is the opposite of
select_by_visible_text( ) method
Here is a simple program to select “Europe” from the drop down at the AUT.
__author__ = 'rahul' import unittest from selenium import webdriver from selenium.webdriver.support.select import Select class Drpdowm(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() def test_drpdown(self): driver = self.driver driver.maximize_window() driver.get('http://www.toolsqa.com/automation-practice-form/') s1=Select(driver.find_element_by_id('continents')) print(s1.options) for opt in s1.options: s1.select_by_visible_text('Europe') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()