Locator with Selenium

Different Types Of Locators In Selenium WebDriver

Tools & Apps

Selenium is a popular open-source framework, used to perform cross browser testing.
The framework integrates with the most popular browsers, including IE, Safari, Chrome, Edge and Firefox.

If you are familiar with Selenium testing, you will already know that it uses locators to identify elements on a page.
Depending on how the elements are added to the DOM, you can use various strategies to identify and interact with these elements.

This article will focus on the different types of locators and strategies that you can use in your Selenium Automated Tests.

Different types of Locators in Selenium WebDriver

Below is a list of locators that are available with Selenium WebDriver:

  • ID
  • Name
  • ClassName
  • LinkText
  • Partial LinkText
  • TagName
  • CssSelector

ID Locator with Selenium

This is the fastest way to identify an element. You can compare it with document.getElementById in Javascript. It will find the element by an id, assigned to the DOM element.

Name Locator with Selenium

When you use this strategy, Selenium will try to find elements that have a name attribute matching the one you supplied.
If no element is found with the provided name, an exception will be thrown: NoSuchElementException

Link Text Locator with Selenium

Will find elements based on the link text (anchor). If you know there are links on a page, you can use this strategy to find and interact with these elements.
For example, you can click the element to proceed to the next page in your test.

Partial Link Text Locator with Selenium

This is a similar strategy to the link text strategy, but in this case, any element that matches a piece of the link text that was provided will be returned.
If you know there’s a link but its anchor text might change, you can use this strategy to try and identify it.

TagName

Locator with Selenium

This locator will find all DOM elements that have a specific tag. You can use this to for example search all DOM elements that are h1, or span, or any other tag.

CSS Selector Locator with Selenium

Finds elements using a CSS selector. You can use these locators to search for elements on a page that have a specific CSS class or ID.

Conclusion

Selenium is a powerful tool to run automated tests against websites. It has a bit of a learning curve, but once you know the various features it offers, it’s an invaluable tool for any QA and developer.

Leave a Reply

Your email address will not be published. Required fields are marked *