Pytest + Selenium Selector Type Cheat Sheet
Handy Guide to Have for find_elements
When using Pytest with Selenium, knowing which selector strategy to use can save time and boost test reliability. This cheat sheet summarizes the most common selector types along with syntax examples and practical use cases.
Below is a handy reference image you can download and keep for quick access during test writing.

Common Selector Examples
# ID
driver.find_element(By.ID, "login") # Fast and most reliable
# Name
driver.find_element(By.NAME, "username") # Often used in forms
# Class Name
driver.find_element(By.CLASS_NAME, "btn-primary") # Good for unique CSS classes
# Tag Name
driver.find_element(By.TAG_NAME, "input") # Generic, works when no better option exists
# CSS Selector
driver.find_element(By.CSS_SELECTOR, ".menu > li") # Flexible, powerful, great for nested elements
# XPath
driver.find_element(By.XPATH, "//div[@id='main']") # Very powerful for complex DOMs
# Link Text
driver.find_element(By.LINK_TEXT, "Sign in") # When matching exact visible link text
# Partial Link Text
driver.find_element(By.PARTIAL_LINK_TEXT, "Sign") # For dynamic or lengthy link text
Understanding the strengths of each selector type can help you write more stable and maintainable Selenium tests. Keep this cheat sheet nearby during your next automation sprint.