QA Graphic

Input and Select Cheat Sheet

Handy Reference to add text when running Automation

Automating user input and form interactions is a core part of UI testing with Selenium. This cheat sheet provides quick reference examples for entering text and interacting with dropdown menus in a Pytest test suite.

Typing into Input Fields


from selenium.webdriver.common.by import By
# Find input by ID and enter text
username_input = driver.find_element(By.ID, "username")
username_input.clear()
username_input.send_keys("test_user")
# Find input by Name attribute
email_input = driver.find_element(By.NAME, "email")
email_input.send_keys("user@example.com")
# Input into a field using CSS selector
password_input = driver.find_element(By.CSS_SELECTOR, "input[type='password']")
password_input.send_keys("securePassword123")
        

Working with Select Menus

Selenium provides a helper class called Select for interacting with dropdowns built using the HTML <select> tag.


from selenium.webdriver.support.ui import Select
# Select option by visible text
dropdown = Select(driver.find_element(By.ID, "country"))
dropdown.select_by_visible_text("Canada")
# Select option by index
dropdown.select_by_index(2)
# Select option by value
dropdown.select_by_value("us")
        

Validating Input Values


# Assert input value is what we typed
assert username_input.get_attribute("value") == "test_user"
        

Bonus: Handling Auto-Suggest Inputs


# Type and wait for suggestion list, then select item
search_box = driver.find_element(By.ID, "search")
search_box.send_keys("Pytest")
# Wait and click suggestion
suggestion = driver.find_element(By.XPATH, "//li[contains(text(), 'Pytest Tutorial')]")
suggestion.click()
        

These examples serve as a solid foundation for common input tasks in your Selenium test cases. Keep this guide handy when building tests involving forms, login fields, or dropdown menus.

 

Comments

Add Your Comments