About this lesson
Selenium Python multi-select dropdown: deselect_by_visible_text and reading what remains
Selenium Python AssignmentsIntermediateDropdowns
How it works
Some dropdowns carry the multiple attribute, so a user can hold several options at once. Selenium models this with the same Select class. The is_multiple attribute tells you which kind you have. It is True here and None on a single-select, so is_multiple() with brackets raises TypeError.
In a multi-select, select_by_visible_text adds to the selection. deselect_by_visible_text removes one entry and leaves the rest. On a normal single-select the deselect methods raise NotImplementedError ("You may only deselect ... of a multi-select"), so check is_multiple if you are unsure.
all_selected_options returns a list in document order, not click order. Pick Cypress before Selenium and Selenium still comes first, because it comes first in the HTML. With nothing selected the list is empty, but first_selected_option raises NoSuchElementException ("No options are selected").
To print the labels on one line you need strings, and a list of elements is not one. A generator expression over the elements, passed to ", ".join(...), builds the text.
About the assignment
Prove the second select allows multiple choices, pick two frameworks, show both, drop one, and show what is left.
What you will do
- Wrap the multi-select element and print whether it allows multiple choices. Convert the boolean with
str(). - Select Selenium and Cypress by visible text, then print the labels of all selected options joined by a comma and a space.
- Deselect Cypress by visible text.
- Print the selected labels again, joined the same way, so only one name remains.
Common mistakes
Joining elements instead of text
Passing
all_selected_optionsstraight tojoinraisesTypeError: sequence item 0: expected str instance, WebElement found. Take each element'stextfirst.Adding the boolean to a string
A label plus
is_multipleraises aTypeErrorthat namesbool. Wrap the property instr().Deselecting on the wrong dropdown
The single-select from earlier lessons is not a multi-select. A deselect call on it raises
NotImplementedError. Check the element id before you wrap it.
In a real test suite
Real forms use multi-selects for tags, roles or filters. A helper such as select_many(select, labels) loops over a list of names so every test uses one tidy call, and a fixture may call deselect_all to reset the control.
The next lesson goes back to a normal dropdown and reads the option the page chose for you.
Related lessons
- Choose a Dropdown Option by Visible TextChoose an option by visible text in a single-select, the same method used here.
- Read the Option That Is Already SelectedRead the option that a dropdown already has selected when it loads.
- Clear a Checkbox That Starts SelectedUndo a selection that was already made, this time on a checkbox.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/Dropdowns")
# Select Selenium and Cypress, then deselect Cypress
finally:
driver.quit()