When working with Selenium WebDriver, one of the most crucial skills is the ability to locate and interact with web elements effectively. In the last tutorial we learnt how to write the first script in Selenium. This tutorial will provide an exhaustive guide on how to use various locators in Selenium with Python, including ID, CSS, XPath, and other identifiers. We’ll also discuss the precedence of these locators and best practices for using them.

Selenium Locators: Complete Guide

Table of Contents

  1. Understanding Locators
  2. Types of Locators
  3. Locator Precedence
  4. Best Practices
  5. Conclusion

Understanding Locators

Locators are methods used to identify and find elements on a web page. Selenium WebDriver provides several strategies to locate elements, each with its own advantages and use cases.

Types of Locators

ID Locator

The ID locator is the most preferred and efficient way to locate an element on a web page. It uses the id attribute of an HTML element.


from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
element = driver.find_element(By.ID, "login-button")

Name Locator

The Name locator uses the name attribute of an HTML element.


element = driver.find_element(By.NAME, "username")

Class Name Locator

The Class Name locator uses the class attribute of an HTML element.


element = driver.find_element(By.CLASS_NAME, "submit-button")

Tag Name Locator

The Tag Name locator finds elements by their HTML tag name.


elements = driver.find_elements(By.TAG_NAME, "input")

The Link Text locator finds anchor (<a>) elements by their exact text.


element = driver.find_element(By.LINK_TEXT, "Click here")

Similar to Link Text, but matches partial text.


element = driver.find_element(By.PARTIAL_LINK_TEXT, "Click")

CSS Selector

CSS Selectors are a powerful way to locate elements using CSS syntax.


element = driver.find_element(By.CSS_SELECTOR, "#login-form input[name='username']")

Common CSS Selector patterns:

  • #id: Selects elements by id
  • .class: Selects elements by class
  • element: Selects elements by tag name
  • [attribute='value']: Selects elements with specific attribute values

XPath

XPath is the most flexible locator strategy, allowing you to navigate through the HTML structure.


element = driver.find_element(By.XPATH, "//input[@id='username']")

Common XPath patterns:

  • //tag[@attribute='value']: Selects elements with specific attributes
  • //tag[contains(@attribute, 'value')]: Selects elements containing specific attribute values
  • //tag[text()='text']: Selects elements with specific text content
  • //parent-tag/child-tag: Selects child elements of a parent

Locator Precedence

When choosing locators, it’s important to consider their reliability and performance. Here’s a general order of preference:

  1. ID
  2. Name
  3. CSS Selector
  4. XPath
  5. Link Text / Partial Link Text
  6. Tag Name
  7. Class Name

This order is based on uniqueness and performance. IDs are typically unique and fast to locate, while class names can be less specific and potentially slower.

Best Practices

  1. Use IDs when possible: They’re unique and efficient.
  2. Prefer CSS Selectors over XPath: They’re generally faster and more readable.
  3. Avoid using indexes in XPath: They’re fragile and can break easily if the page structure changes.
  4. Use meaningful locators: Choose locators that describe the element’s purpose or content.
  5. Keep locators as short and simple as possible: This improves readability and maintenance.
  6. Use relative XPaths instead of absolute paths: They’re more resilient to changes in the page structure.
  7. Regularly update and maintain your locators: Web pages change, so should your locators.
The more precisely you can target an element, the less DOM traversal WebDriver needs to perform, resulting in faster and more stable test execution. Specific locators not only improve performance but also enhance the robustness of your automation scripts.

What do you think?

Mastering element location is crucial for effective Selenium automation. By understanding and properly using various locator strategies, you can create more robust and efficient test scripts. Remember to choose the most appropriate locator for each situation, considering both uniqueness and performance.

As you continue to work with Selenium, you’ll develop a sense for which locators work best in different scenarios. Practice and experimentation will help you become proficient in finding and interacting with web elements effectively. Do let us know your thoughts on locators in Selenium.