QA Graphic

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.

Pytest Selenium Selector Cheat Sheet

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.

 

Comments

Add Your Comments

Name:
Comment:

 

About

Welcome to Pytest Tips and Tricks, your go-to resource for mastering the art of testing with Pytest! Whether you're a seasoned developer or just dipping your toes into the world of Python testing, this blog is designed to help you unlock the full potential of Pytest - one of the most powerful and flexible testing frameworks out there. Here, I'll share a treasure trove of practical insights, clever techniques, and time-saving shortcuts that I've gathered from years of writing tests and debugging code.

Schedule

Wednesday 18 Pytest
Thursday 19 PlayWright
Friday 20 Macintosh
Saturday 21 Internet Tools
Sunday 22 Misc
Monday 23 Media
Tuesday 24 QA