What are the element locators in Selenium WebDriver?

Selenium is an open-source web-based automation tool. In the world of web automation testing, Selenium WebDriver is the undisputed champion. It allows testers and developers to interact with web applications just like a real user. One crucial aspect of Selenium's power lies in its ability to locate and manipulate web elements on a page. We’ll learn how to find elements by using different element locators of the Selenium web driver in Python.

What are the element locators?

Element locators are like the GPS for Selenium WebDriver. They guide the tool to precisely identify and interact with specific elements on a web page. Imagine trying to use a smartphone without any location services. It would be challenging, right? Similarly, without effective element locators, the Selenium tests won’t get very far.

Common types of element locators

Selenium WebDriver provides various element locators, each with its unique way of pinpointing elements. Here are some of the most common ones:

  • ID: This is one of the most robust locators, as it should be unique for each element on a page.

  • Name: If an element has a name attribute, we can use it as a locator.

  • Class name: We can use the class name to locate elements that share the same styling or behavior.

  • CSS selector: CSS selectors offer powerful ways to locate elements based on their attributes and relationships in the DOM.

  • Link text: It is a reliable choice for pinpointing anchor (link) elements by their visible text. It helps to interact precisely with hyperlinks, making it an invaluable asset for navigating web pages.

  • Partial link text: It enables us to locate links by specifying only a portion of the text, providing flexibility in handling ever-changing or extended link labels.

  • XPath: XPath is incredibly flexible and can traverse the entire DOM to find an element.

Syntax

Let’s look at how we can use these element locators to target our desired elements on any web page:

# Extracting element using Name locator
driver.find_element(By.NAME, 'provide the name of the element')
# Extracting element using Link Text locator
driver.find_element(By.LINK_TEXT, 'provide complete text of a link')
# Extracting element using Partial Link Text locator
driver.find_element(By.PARTIAL_LINK_TEXT, 'provide partial text of a link')

Example

Let’s take a look at an example of using element locators to navigate to the desired part of the web page:

# importing libraries
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep

# webdriver setup
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

# Setting the root as the download directory.
# You can change it to, for example, "D:\Selenium\Test".
prefs = {"download.default_directory": "."};

# Firing up webdriver
options.add_experimental_option("prefs", prefs);
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

# navigating to the website
driver.get('https://www.python.org/');
sleep(10)

# accessing the Documentation tab using partial link text
partial_link_text = "Docs"
element = driver.find_element(By.PARTIAL_LINK_TEXT, partial_link_text)
element.click()

sleep(60);
driver.close();
Selenium webdriver

Explanation

  • Lines 2–7: We import the necessary libraries to run the selenium webdriver.

  • Lines 19–20: We set up our web driver, which is a chromedriver in this case.

  • Line 23: We navigate to any website (python.org), in this case, by using driver.get() method.

  • Lines 27–29: We use PARTIAL_LINK_TEXT element locator to locate the desired elements of our sample web page.

  • Line 32: We close the web driver instance by calling the driver.close() method once all the processing is completed.

Best practices for using element locators

Understanding the various types of element locators is essential, but it’s equally crucial to use them effectively. Here are some best practices:

  • Prioritize ID and name: Whenever possible, we try to use ID or name locators. They are fast and reliable.

  • Avoid using XPath: While XPath is powerful, it can be slower and more brittle than other locators. We only head to XPath if no other option is left.

  • Dynamic elements: In the case of dynamic elements, we try to gracefully handle them by using strategies like partial matching or regular expressions.

  • Page load delays: We have to be mindful of page load times, as attempting to locate an element before it’s available can lead to test failures.

Unlock your potential: Selenium WebDriver series, all in one place!

To continue your exploration of Selenium WebDriver, check out our series of Answers below:

Free Resources