DeSelect Methods

Let's discuss one by one of all deselect methods which is supported on multiselect options.


MultiSelect

is_multiple property:

This property is used to verify whether WebElement supports multiselect or not.By using a select class object.It doesn’t take any argument but returns boolean value true or false.

 
ele = wait.until(ec.presence_of_element_located((By.ID, "multiselect")))

# Create the object for Select class
ms_options = Select(ele)

print("Check whether it is a multi select or not : ", ms_options.is_multiple)


all_selected_options property:

This property is used to get all selected options.It doesn’t take any argument but returns list value.

As we discussed earlier in the dropdown chapter.All those select methods are used in this multiselect operation.

 
ele = wait.until(ec.presence_of_element_located((By.ID, "multiselect")))

# Create the object for Select class
ms_options = Select(ele)

selected_option = ms_options.all_selected_options
print("List out all selected options : ", selected_option)

deselect_by_index() Method:

This method is used to deSelect the option using index value.It takes integer value as an argument and it doesn’t return anything.

ms_options.select_by_index(1)

deselect_by_value() Method:

This method is used to deSelect the option using the value of multi select option.It takes String as an argument and it doesn’t return anything.

ms_options.deselect_by_value("mOptionTWo")


MultiSelect

deselect_by_visible_text() Method :

This method is used to deSelect the option using the text value of multi select option.It takes String as an argument and it doesn’t return anything.

ms_options.deselect_by_visible_text("mOption3")


MultiSelect


Example:

 

Deselect_options.py

from selenium import webdriver
import time
from selenium.common.exceptions import ElementNotVisibleException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe")

driver.get("http://www.dummypoint.com/seleniumtemplate.html")
time.sleep(2)

wait = WebDriverWait(driver, 25, poll_frequency=1,
                     ignored_exceptions=[ElementNotVisibleException, NoSuchElementException])
ele = wait.until(ec.presence_of_element_located((By.ID, "multiselect")))


# import the Select class

# Create the object for Select class
ms_options = Select(ele)

print("Check whether it is a multi select or not : ", ms_options.is_multiple)

# List the values in Multi Select
ms_v = ms_options.options
for ms_value in ms_v:
    print(ms_value.text)

# Click by Index
ms_options.select_by_index(1)
# Click by Value
ms_options.select_by_value("mOptionTWo")
# Click by Text
ms_options.select_by_visible_text("mOption3")

time.sleep(2)

selected_option = ms_options.all_selected_options
print("List out all selected options : ", selected_option)

# deselect_by_index
ms_options.deselect_by_index(1)
time.sleep(2)

# deselect_by_value
ms_options.deselect_by_value("mOptionTWo")
time.sleep(2)

# deselect_by_visible_text
ms_options.deselect_by_visible_text("mOption3")

time.sleep(5)
driver.quit()