0
0
Selenium Pythontesting~15 mins

Select by value, text, index in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify dropdown selection by value, visible text, and index
Preconditions (1)
Step 1: Locate the dropdown menu with id 'country-select'
Step 2: Select the option with value 'us' and verify the selected option is 'United States'
Step 3: Select the option with visible text 'Canada' and verify the selected option's value is 'ca'
Step 4: Select the option at index 3 and verify the selected option's text is 'Mexico'
✅ Expected Result: Dropdown selections by value, text, and index work correctly and the selected options match the expected values and texts.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert selected option text after selecting by value
Assert selected option value after selecting by visible text
Assert selected option text after selecting by index
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 element selection
Use assert statements for verification
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Setup WebDriver (assuming chromedriver is in PATH)
driver = webdriver.Chrome()

try:
    driver.get('https://example.com/dropdown')  # Replace with actual URL

    wait = WebDriverWait(driver, 10)
    dropdown_element = wait.until(EC.presence_of_element_located((By.ID, 'country-select')))

    select = Select(dropdown_element)

    # Select by value 'us'
    select.select_by_value('us')
    selected_option = select.first_selected_option
    assert selected_option.text == 'United States', f"Expected 'United States', got '{selected_option.text}'"

    # Select by visible text 'Canada'
    select.select_by_visible_text('Canada')
    selected_option = select.first_selected_option
    assert selected_option.get_attribute('value') == 'ca', f"Expected value 'ca', got '{selected_option.get_attribute('value')}'"

    # Select by index 3
    select.select_by_index(3)
    selected_option = select.first_selected_option
    assert selected_option.text == 'Mexico', f"Expected 'Mexico', got '{selected_option.text}'"

finally:
    driver.quit()

This script uses Selenium WebDriver with Python to automate the dropdown selection test.

First, it opens the webpage and waits explicitly for the dropdown element with id 'country-select' to be present.

It creates a Select object to interact with the dropdown options.

Then it selects options by value, visible text, and index respectively, verifying after each selection that the correct option is selected by checking the option's text or value attribute.

Assertions ensure the test fails if the selected option does not match expectations.

Finally, the browser is closed to clean up.

Common Mistakes - 3 Pitfalls
Using find_element without explicit wait
{'mistake': 'Selecting dropdown options by clicking option elements directly', 'why_bad': "This can be unreliable and harder to maintain compared to using Selenium's Select class.", 'correct_approach': "Use Selenium's Select class methods like select_by_value, select_by_visible_text, and select_by_index."}
Using hardcoded XPath for dropdown without stable attributes
Bonus Challenge

Now add data-driven testing with 3 different sets of selection inputs: (value='us', expected text='United States'), (text='Canada', expected value='ca'), (index=3, expected text='Mexico')

Show Hint