Skip to content
CodeKruPro

Select and Deselect in a Multi-Select

33/80

Select and Deselect in a Multi-Select

is_multiple tells you whether more than one option can be selected. deselect_by_visible_text clears one of them. The selected options stay in DOM order.

Task:

  1. Print whether #multiSelect allows multiple selections
  2. Select Selenium and Cypress and print them
  3. Deselect Cypress and print what remains

Print the lines below exactly.

Expected output:

Is multiple: True
Selected: Selenium, Cypress
After deselect: Selenium

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

  1. Wrap the multi-select element and print whether it allows multiple choices. Convert the boolean with str().
  2. Select Selenium and Cypress by visible text, then print the labels of all selected options joined by a comma and a space.
  3. Deselect Cypress by visible text.
  4. Print the selected labels again, joined the same way, so only one name remains.

Common mistakes

  • Joining elements instead of text

    Passing all_selected_options straight to join raises TypeError: sequence item 0: expected str instance, WebElement found. Take each element's text first.

  • Adding the boolean to a string

    A label plus is_multiple raises a TypeError that names bool. Wrap the property in str().

  • 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.

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.

main.py
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()