0
0
Selenium Pythontesting~15 mins

Multi-select handling in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify multi-select dropdown selection and deselection
Preconditions (1)
Step 1: Locate the multi-select dropdown by its id 'fruits'
Step 2: Select the options 'Apple' and 'Banana' by visible text
Step 3: Verify that both 'Apple' and 'Banana' are selected
Step 4: Deselect the option 'Apple'
Step 5: Verify that only 'Banana' remains selected
✅ Expected Result: The multi-select dropdown allows selecting multiple options. After deselecting 'Apple', only 'Banana' remains selected.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that 'Apple' and 'Banana' options are selected after selection
Assert that only 'Banana' is selected after deselecting 'Apple'
Best Practices:
Use Selenium's Select class for multi-select dropdown handling
Use explicit waits if necessary before interacting
Use clear and maintainable locators (By.ID)
Use assertions from unittest or pytest for validation
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import unittest

class TestMultiSelect(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/multiselect')  # Replace with actual URL

    def test_multi_select_handling(self):
        driver = self.driver
        select_element = driver.find_element(By.ID, 'fruits')
        select = Select(select_element)

        # Select 'Apple' and 'Banana'
        select.select_by_visible_text('Apple')
        select.select_by_visible_text('Banana')

        # Verify both are selected
        selected_options = [opt.text for opt in select.all_selected_options]
        self.assertIn('Apple', selected_options, "Apple should be selected")
        self.assertIn('Banana', selected_options, "Banana should be selected")

        # Deselect 'Apple'
        select.deselect_by_visible_text('Apple')

        # Verify only 'Banana' remains selected
        selected_options_after = [opt.text for opt in select.all_selected_options]
        self.assertNotIn('Apple', selected_options_after, "Apple should be deselected")
        self.assertIn('Banana', selected_options_after, "Banana should still be selected")

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

This test uses Selenium WebDriver with Python's unittest framework.

In setUp, the browser opens the target page with the multi-select dropdown.

We locate the dropdown by its id='fruits' and create a Select object to handle multi-select options easily.

We select 'Apple' and 'Banana' by their visible text, then check that both appear in the list of selected options.

Next, we deselect 'Apple' and verify that only 'Banana' remains selected.

Assertions ensure the test fails if the selections do not match expectations.

Finally, tearDown closes the browser to clean up.

Common Mistakes - 4 Pitfalls
{'mistake': "Using find_element and clicking options instead of using Selenium's Select class", 'why_bad': 'Manually clicking options is error-prone and less readable; Select class provides clear methods for multi-select handling.', 'correct_approach': "Use Selenium's Select class to select and deselect options by visible text or value."}
Not verifying the selected options after selection or deselection
Using hardcoded XPath locators that are brittle
Not closing the browser after test execution
Bonus Challenge

Now add data-driven testing to select and deselect multiple sets of fruits: ['Apple', 'Banana'], ['Orange', 'Grape'], ['Banana', 'Grape']

Show Hint