0
0
Selenium Pythontesting~5 mins

Multi-select handling in Selenium Python

Choose your learning style9 modes available
Introduction

Multi-select handling lets you choose many options from a list on a webpage. It helps test if the website works well when users pick multiple items.

Testing a form where users can select multiple hobbies or interests.
Verifying a filter on a shopping site that allows selecting several brands at once.
Checking if a survey lets users pick more than one answer.
Validating a settings page where multiple notification types can be enabled.
Ensuring a multi-select dropdown correctly shows all chosen options.
Syntax
Selenium Python
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

select = Select(driver.find_element(By.ID, 'element_id'))
select.select_by_visible_text('Option Text')
select.select_by_value('option_value')
select.select_by_index(index)

Use Select class to work with multi-select dropdowns.

Make sure the HTML <select> element has the multiple attribute.

Examples
Selects the option that shows 'Apple' to the user.
Selenium Python
select.select_by_visible_text('Apple')
Selects the option with the value attribute 'banana'.
Selenium Python
select.select_by_value('banana')
Selects the third option in the list (index starts at 0).
Selenium Python
select.select_by_index(2)
Deselects all selected options in a multi-select list.
Selenium Python
select.deselect_all()
Sample Program

This script opens a test page with a multi-select list of car brands. It selects 'Volvo' and 'Opel', prints the selected options, then deselects all and prints again to confirm.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time

# Setup driver (make sure chromedriver is in PATH)
driver = webdriver.Chrome()
driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple')

# Switch to iframe where the multi-select is
driver.switch_to.frame('iframeResult')

# Locate multi-select element
select_element = driver.find_element(By.NAME, 'cars')
select = Select(select_element)

# Select multiple options
select.select_by_visible_text('Volvo')
select.select_by_visible_text('Opel')

# Verify selections
selected_options = [opt.text for opt in select.all_selected_options]
print('Selected options:', selected_options)

# Deselect all
select.deselect_all()

# Verify deselection
selected_after_deselect = [opt.text for opt in select.all_selected_options]
print('Selected after deselect:', selected_after_deselect)

time.sleep(2)
driver.quit()
OutputSuccess
Important Notes

Always check if the select element supports multiple selections by verifying the multiple attribute.

Use deselect_all() only on multi-select elements; it will cause an error on single-select dropdowns.

Switch to the correct iframe if the multi-select is inside one before locating the element.

Summary

Multi-select handling lets you pick many options from a list in tests.

Use Selenium's Select class with methods like select_by_visible_text and deselect_all.

Always confirm the element supports multiple selections before using multi-select methods.