Preguntas y respuestas de entrevista mas solicitadas y pruebas en linea
Plataforma educativa para preparacion de entrevistas, pruebas en linea, tutoriales y practica en vivo

Desarrolla tus habilidades con rutas de aprendizaje enfocadas, examenes de practica y contenido listo para entrevistas.

WithoutBook reune preguntas de entrevista por tema, pruebas practicas en linea, tutoriales y guias comparativas en un espacio de aprendizaje responsivo.

Chapter 3

Locators: ID, Name, CSS, XPath, and Best Practices

Learn how Selenium finds elements and why locator strategy is one of the most important factors in stable test automation.

Inside this chapter

  1. Why Locators Are So Important
  2. Common Locator Strategies
  3. Basic Locator Example
  4. CSS vs XPath
  5. Best Practices for Stable Locators
  6. Real-Time Example

Series navigation

Study the chapters in order for the clearest path from Selenium setup and locators to framework design, CI integration, flaky-test control, and advanced automation engineering practice. Use the navigation at the bottom to move smoothly through the full tutorial series.

Tutorial Home

Chapter 3

Why Locators Are So Important

If Selenium cannot identify the correct element reliably, the test becomes fragile. Strong locator strategy is the foundation of maintainable browser automation. Many flaky suites fail not because Selenium is weak, but because element identification is poorly designed.

Chapter 3

Common Locator Strategies

  • ID
  • Name
  • Class name
  • Tag name
  • Link text and partial link text
  • CSS selector
  • XPath
Chapter 3

Basic Locator Example

driver.findElement(By.id("email")).sendKeys("student@example.com");
driver.findElement(By.cssSelector("button[type='submit']")).click();
Chapter 3

CSS vs XPath

CSS selectors are often simpler and faster to read. XPath can be more expressive for complex tree relationships. Strong engineers know both, but they prefer the simplest reliable locator rather than the most clever one.

Chapter 3

Best Practices for Stable Locators

  • Prefer stable IDs or test-specific attributes where possible
  • Avoid fragile absolute XPath
  • Avoid locators based on changing style classes only
  • Collaborate with developers to add automation-friendly attributes
  • Keep locators readable and intention-revealing
Chapter 3

Real-Time Example

An e-commerce search test may fail every sprint if it locates the search input through a deeply nested absolute XPath tied to layout changes. The same test may remain stable for months if it uses a dedicated test id or semantic selector.

Copyright © 2026, WithoutBook.