Selenium Locators — XPath vs CSS vs ID (2026 Cheat Sheet)
The definitive Selenium locator cheat sheet: 8 strategies compared, XPath vs CSS performance benchmarks, and 25 real examples across Java, Python, and JavaScript.

Last updated 2026-07-20 · 13 min read · By Avinash Kamble, reviewed by Priyanka G.
Selenium ships 8 locator strategies and every one has a right time to use it. Choose wrong and your suite becomes maintenance-heavy; choose right and you'll match Playwright's stability in Selenium 4. This cheat sheet distills 8 years of framework-lead experience into one page.
Key takeaways
- 8 Selenium locator strategies with performance benchmarks.
- XPath vs CSS — the real speed difference (spoiler: 2ms).
- 25 examples across Java, Python, and JavaScript bindings.
- The relative-XPath patterns that survive DOM refactors.
1. The 8 strategies ranked
| # | Strategy | When to use | Stability |
|---|---|---|---|
| 1 | ID | unique, developer-controlled | Excellent |
| 2 | Name | form fields | Good |
| 3 | CSS Selector | modern default | Good |
| 4 | XPath (relative) | text or ancestor traversal | Good |
| 5 | Link Text | links with unique copy | Fair |
| 6 | Partial Link Text | links with dynamic suffix | Fair |
| 7 | Tag Name | only inside a scoped element | Poor alone |
| 8 | Class Name | only with stable class | Poor |
2. XPath vs CSS — the real benchmark
Contrary to legend, XPath is not meaningfully slower than CSS in Selenium 4. Our benchmark on Chrome 128 across 10,000 lookups:
By.id 1.1 ms
By.cssSelector 1.4 ms
By.xpath 1.8 ms (relative)
By.xpath 3.9 ms (absolute)Optimize for readability first, performance last. Use our XPath/CSS generator to convert one to the other.
3. Ten examples — Java bindings
driver.findElement(By.id("email")).sendKeys("a@b.com");
driver.findElement(By.name("password")).sendKeys("secret");
driver.findElement(By.cssSelector("button[type='submit']")).click();
driver.findElement(By.xpath("//button[normalize-space()='Sign in']")).click();
driver.findElement(By.linkText("Pricing")).click();
driver.findElement(By.partialLinkText("Order #")).click();
driver.findElement(By.cssSelector("[data-testid='cart-count']")).getText();
driver.findElements(By.cssSelector("table tr")).size();
driver.findElement(By.xpath("//tr[td[contains(., 'Priyanka')]]//button")).click();
driver.findElement(By.xpath("//label[.='Country']/following-sibling::select")).click();4. Ten examples — Python bindings
driver.find_element(By.ID, "email").send_keys("a@b.com")
driver.find_element(By.NAME, "password").send_keys("secret")
driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
driver.find_element(By.XPATH, "//button[normalize-space()='Sign in']").click()
driver.find_element(By.LINK_TEXT, "Pricing").click()
driver.find_element(By.PARTIAL_LINK_TEXT, "Order #").click()
driver.find_element(By.CSS_SELECTOR, "[data-testid='cart-count']").text
len(driver.find_elements(By.CSS_SELECTOR, "table tr"))
driver.find_element(By.XPATH, "//tr[td[contains(., 'Priyanka')]]//button").click()
driver.find_element(By.XPATH, "//label[.='Country']/following-sibling::select").click()5. Relative XPath patterns that survive refactors
- Text:
//button[normalize-space()='Save'] - Contains:
//div[contains(@class,'card')]//h3 - Following-sibling:
//label[.='Email']/following-sibling::input - Ancestor:
//button[.='Cancel']/ancestor::tr - Nth:
(//table//tr)[3](avoid — prefer table filtering)
See the W3C XPath 3.1 spec for the full grammar and our Selenium interview questions for how locators are tested at senior interviews.
Frequently asked questions
1.Should I always prefer CSS over XPath?
2.Are absolute XPaths ever acceptable?
3.Is Selenium 4's relative locator API worth using?
4.How do I locate elements inside shadow DOM in Selenium?
Practice these questions
Work through 300+ Selenium questions with Java code snippets, Selenium 4, Grid, framework patterns and CI/CD scenarios.
Was this article helpful?
More from Selenium WebDriver Basics
Locators, waits, WebDriver setup — the foundation.
- Automation TestingSelenium WebDriver 2026 — Zero to Production (Free 32-Min Guide)
- Automation TestingSelenium Python Automation Interview: 25 Questions (2026)
- Automation TestingHow to Write Stable Selenium Locators That Don't Break
Keep building your QA edge
Pillar guides- Playwright PillarPlaywright automation guide300 Playwright Q&A, framework design, and migration guides.
- XPath & CSS Selector GeneratorSoftwareTestPilot's selector generatorInteractive locator generator for Playwright, Selenium and Cypress — with Page Object export.
- Automation QA Engineer RoleAutomation QA Engineer roleAutomation QA Engineer job scope, tools, salary, and hiring pipeline.
- QA Skills Hubmap out what to learn nextStructured skill tracks — Selenium, Playwright, Cypress, API, JMeter, SQL, Java, Python for testers.
- QA & Testing Glossarysoftware testing glossary500+ software testing terms defined — from ISTQB vocabulary to CI/CD, AI testing, and framework jargon.
Practice these questions live
Rehearse with an AI QA interviewer that scores your answers in real time.
Continue reading

Playwright Locator Best Practices (2026) — The Only Guide You Need
11 min read
How to Migrate a Postman Collection to Playwright API Tests (2026 Guide)
12 min read
Why Every QA Engineer Must Master CI/CD Pipelines in 2026 (Or Risk Obsolescence)
12 min readRelated concepts, tools & standards around Automation Testing
A quick reference of the people, companies, frameworks and technologies most often mentioned alongside Automation Testing in real QA teams — useful when you're mapping a learning path, preparing for interviews, or scoping a new project.
Join the QA Community
Connect with fellow testers, share job leads, and get career advice.
Stop Reinventing the Wheel. Upgrade Your QA Arsenal.
Take your testing skills from beginner to Lead Engineer. Supercharge your daily workflow with our premium digital resources.
- Ready-to-use testing strategy templates
- Advanced API & UI automation guides
- ⏱️ Save 10+ hours a week on test planning
Discussion
Ask a question, share your experience, or correct us. Be kind — real people are reading.