Selenium Locators: A Complete Guide to All Types of Locators with Code Examples



selenium stale element exception

Selenium is a popular open-source test automation framework used for automating web applications.

One of the key features of Selenium is its ability to interact with web elements on a page using different types of locators.

Locators are used to identify web elements on a page, such as buttons, text boxes, links, etc., and perform actions on them.

In this article, we will discuss the different types of locators in Selenium and how to use them in your test automation scripts.

ID Locator

The ID locator is the most reliable and preferred locator strategy in Selenium. It uses the "id" attribute of the HTML tag to locate web elements. Here's an example:

WebElement element = driver.findElement(By.id("element-id"));
Name Locator

The Name locator strategy uses the "name" attribute of the HTML tag to locate web elements. Here's an example:

WebElement element = driver.findElement(By.name("element-name"));
Class Name Locator

The Class Name locator strategy uses the "class" attribute of the HTML tag to locate web elements. Here's an example:

WebElement element = driver.findElement(By.className("element-class"));
Tag Name Locator

The Tag Name locator strategy uses the HTML tag name to locate web elements. Here's an example:

WebElement element = driver.findElement(By.tagName("element-tag"));
Link Text Locator

The Link Text locator strategy is used to locate links on a web page. It uses the text of the link to locate the element. Here's an example:

WebElement element = driver.findElement(By.linkText("element-link-text"));
Partial Link Text Locator

The Partial Link Text locator strategy is used to locate links on a web page by matching a part of the link text. Here's an example:

WebElement element = driver.findElement(By.partialLinkText("partial-link-text"));
CSS Selector Locator

The CSS Selector locator strategy uses CSS selectors to locate web elements. Here's an example:

WebElement element = driver.findElement(By.cssSelector("element-css-selector"));
XPath Locator

The XPath locator strategy uses XPath expressions to locate web elements. Here's an example:

WebElement element = driver.findElement(By.xpath("element-xpath"));
Now that we've covered all the different types of locators in Selenium, let's look at a few best practices for using them.

Best Practices for Using Selenium Locators:



  • Use the ID locator whenever possible as it is the most reliable and efficient locator strategy.
  • Avoid using the Tag Name locator as it may not be unique on a web page.
  • Use the CSS Selector or XPath locator strategies when you need to locate web elements based on their attributes or properties.
  • Use the Link Text and Partial Link Text locator strategies to locate links on a web page.
  • Use the Name and Class Name locator strategies only if there are no other unique locators available.


In conclusion, understanding the different types of locators in Selenium and how to use them is crucial for writing effective and efficient test automation scripts. By following the best practices outlined in this article, you can ensure that your Selenium locators are reliable and robust.





Read Next :






Code Camp!
Register for a free code camp.
email: info@techoral.com