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.
Multi-select handling in 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.
select.select_by_visible_text('Apple')select.select_by_value('banana')select.select_by_index(2)select.deselect_all()
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.
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()
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.
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.