0
0
Selenium Pythontesting~15 mins

Select class for dropdowns in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify selecting options from a dropdown using Select class
Preconditions (2)
Step 1: Locate the dropdown element by id 'country-select'.
Step 2: Use the Select class to select the option 'Canada' by visible text.
Step 3: Verify that the selected option is 'Canada'.
Step 4: Use the Select class to select the option with value 'mexico'.
Step 5: Verify that the selected option is 'Mexico'.
Step 6: Use the Select class to select the option by index 0.
Step 7: Verify that the selected option is 'USA'.
✅ Expected Result: The dropdown selection changes correctly to 'Canada', then 'Mexico', then 'USA' as per the selection method used.
Automation Requirements - selenium
Assertions Needed:
Verify selected option text matches expected after each selection
Best Practices:
Use Selenium's Select class for dropdown interactions
Use explicit waits to ensure dropdown is present before interacting
Use meaningful locators (By.ID) for dropdown element
Use assertions to verify selected option text
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class TestDropdownSelect(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/dropdown')  # Replace with actual URL
        self.wait = WebDriverWait(self.driver, 10)

    def test_select_dropdown_options(self):
        driver = self.driver
        wait = self.wait

        # Wait for dropdown to be present
        dropdown_element = wait.until(EC.presence_of_element_located((By.ID, 'country-select')))

        select = Select(dropdown_element)

        # Select by visible text 'Canada'
        select.select_by_visible_text('Canada')
        selected_option = select.first_selected_option.text
        self.assertEqual(selected_option, 'Canada', f"Expected 'Canada', got '{selected_option}'")

        # Select by value 'mexico'
        select.select_by_value('mexico')
        selected_option = select.first_selected_option.text
        self.assertEqual(selected_option, 'Mexico', f"Expected 'Mexico', got '{selected_option}'")

        # Select by index 0 (should be 'USA')
        select.select_by_index(0)
        selected_option = select.first_selected_option.text
        self.assertEqual(selected_option, 'USA', f"Expected 'USA', got '{selected_option}'")

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

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

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

First, it opens the browser and navigates to the page with the dropdown.

It waits explicitly for the dropdown element with id 'country-select' to be present to avoid timing issues.

Then it creates a Select object to interact with the dropdown.

It selects options by visible text, value, and index, verifying after each selection that the correct option is selected using assertions.

Finally, it closes the browser in tearDown to clean up.

Common Mistakes - 4 Pitfalls
{'mistake': 'Using driver.find_element().click() on dropdown options instead of Select class', 'why_bad': "This can cause unreliable behavior and does not use Selenium's built-in support for dropdowns.", 'correct_approach': "Use Selenium's Select class to select options by visible text, value, or index."}
Not waiting for the dropdown element to be present before interacting
Using hardcoded XPath for dropdown locator instead of stable ID or name
Not verifying the selected option after selection
Bonus Challenge

Now add data-driven testing to select multiple countries from the dropdown using a list of inputs ['Mexico', 'USA', 'Canada'] and verify each selection.

Show Hint