Select Dropdown in Selenium Python

In previous sections, we have discussed about how to work with multiple browsers without downloading the browser driver explicitly and handled locators in selenium with Python. In this section, we will discuss about how to handle Select Dropdown in Selenium Python.

Objective:

  • Introduction
  • Handle Dropdown in Selenium Python
    • By Index
    • Visible text
    • By Value

Introduction:

Handling Select Dropdown element is very easy if we understand well. Consider below to handle dropdown with Selenium Python.

https://www.orangehrm.com/contact-us-2/

Here Country field is Select Dropdown. To know about the details of the Country field. Inspect the field element.

To work with Select Dropdown element in Selenium Python, You need to import the Select from selenium.webdriver.support.ui as below.

from selenium.webdriver.support.ui import Select

After that you can use Select() in your application.

In Select(), we will pass the element of select tag. Then you need to store this to a reference variable. Once you create that you can use several method with your select reference variable.

There are multiple ways you can select the dropdown value. E.g.

  • Select by Index
  • Select By Value
  • Select by Visible text

Let’s discuss one by one.

Select by Index :

Index is the value starts from 0 and continue with +1. If there are 10 dropdown values in the select dropdown then the index will be 0,1,2,3,4,5,6,7,8,9. If you want to click on 4th value from the select dropdown then you need to provide the index as 3.

select.select_by_index(3)

Select by Value :

This represents the value attribute of the dropdown list.  See the below image.

From the image you can see the option tag has the attribute name as value. Whatever the text you will pass to select_by_value as parameter, it will match with the value attribute text and click the dropdown value for which it matched.

select.select_by_value('India')

Select by Visible Text:

This signifies the dropdown value visible in webpage while you click on the dropdown.  See the below image.

Whatever the text you will pass to select_by_visible_text as parameter, it will match with the Dropdown visible text and click the dropdown value for which it matched.

select.select_by_visible_text('India')

Apart from these 3 methods there are other methods also available to work with Select dropdown.

  • Select.is_multiple() is the method which will tell you whether the select dropdown is Multi-Select dropdown or not. Multi-Select dropdown is the one in which you can select more than one element from the dropdown. And Single-Select dropdown is the one in which you can select only one element from the dropdown.
  • Same like above methods, we have deselect method also which will deselect the selected value based on the parameter you passed inside the method.
select.deselect_by_index(3)
select.deselect_by_value('India')
select.deselect_by_visible_text('India')
select.deselect_all()
  • deslect_all will deselected all selected value which are selected in the dropdown.

Let’s have a look on below script.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get("https://www.orangehrm.com/contact-us-2/")
print(driver.title)

indus_ele = driver.find_element(By.ID, 'Form_submitForm_Country')
select = Select(indus_ele)
 select.select_by_value('India')
 

You can see in above image, India is selected.

If you want to handle multiple dropdown, then you need to create multiple variable for different select element. It may occur problem if you will be having larger dropdown which may increase the number of reference variable. Fro example, if you have 40 dropdown in page, then you need to find the element for all 40 dropdown and store in separate reference variable. And you need to use the select method to each reference variable.

This is not a good approach to do. For this we can create a generic method out of it.

For custom method you need to use def. You can learn more about method creation in Python by clicking this link.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
import time


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get("https://www.orangehrm.com/contact-us-2/")
print(driver.title)

def select_value(element, value):
    select = Select(element)
    select.select_by_value(value)

country_ele = driver.find_element(By.ID, 'Form_submitForm_Country')
employee_ele = driver.find_element(By.ID, 'Form_submitForm_NoOfEmployees')
select_value(employee_ele, "16 - 20")
select_value(country_ele, "India")

Explanation:

Here you “select_value” is the custom method which will accept 2 argument. One is element and other is value. Inside that you need to define Select() by passing the element and select_by_value by passing the value. Then you just need to call the method “select_value” by passing element variable and value.

Scenario – 2:

Let’s think a scenario where you need to print all dropdown value. In that case you need to use options method. This select.option will give you the list of dropdown which are available.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
import time


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.implicitly_wait(10)
driver.get("https://www.orangehrm.com/contact-us-2/")
print(driver.title)

def select_value(element, value):
    select = Select(element)
    select.select_by_value(value)

country_ele = driver.find_element(By.ID, 'Form_submitForm_Country')
employee_ele = driver.find_element(By.ID, 'Form_submitForm_NoOfEmployees')

select_value(employee_ele, "16 - 20")
select_value(country_ele, "India")

#select.select_by_index(3)
#select.select_by_value('India')
#select.select_by_visible_text('India')
#select.deselect_all()

select = Select(country_ele)
country_list = select.options
for con in country_list:
    print(con.text)
    if (con.text == 'Zaire'):
        con.click()
        break

Output:

Q. Can we select value from the dropdown list without using Select()?

Yes, We can do that. Fot this we need to use the XPath.

country_list1=driver.find_elements(By.XPATH, '//select[@id="Form_submitForm_Country"]/option')
print(len(country_list))

for con in country_list1:
    print(con1.text)
    if (con.text == 'Algeria'):
        con.click()
        break

Output:

Summary:

Now we have understood how to handle Dropdown in Selenium Python.

  • The text you will pass to select_by_value as parameter, it will match with the value attribute text and click the dropdown value for which it matched.
  • the text you will pass to select_by_visible_text as parameter, it will match with the Dropdown visible text and click the dropdown value for which it matched.
  • Index is the value starts from 0 and continue with +1. If there are 10 dropdown values in the select dropdown then the index will be 0,1,2,3,4,5,6,7,8,9. If you want to click on 4th value from the select dropdown then you need to provide the index as 3.

Leave a Reply

Your email address will not be published. Required fields are marked *