WebDriver – Locators

Locator is a command that tells Selenium IDE/Webdriver which GUI elements needs to operate on (Say Text Box, Buttons, Check Boxes etc). Locators are the basic building blocks of a web page. A web developer must use a proper and consistent locator scheme for a website. Also, a test engineer must choose the correct locator strategy to automate the online workflows.

The following are the locators strategies included in Selenium:

  • Id
  • Name
  • Link Text
  • Partial Link Text
  • XPath
  • Class Name
  • Tag Name
  • Class Name
  1. Id: To select the element with a specified @id attribute

It is a unique reference for a web object that the developer sets while writing the code. Ideally, the ID should not repeat on a page, but the browsers do allow exceptions to this rule. The ID is no doubt the best locator to use in Selenium. Still, if it belongs to an HTML table, then it’s possible that it would change or disappear from the list. Hence, you need to put in a more advanced locator technique.

From the above Screenshot, you can find the element using the locator: ID



2. Name:  To select the first element with the specified @name attribute.

Every form has input fields with unique names. Names are unique most of the times. However, a field name locator is the best choice for testing a login form. But when you have multiple login types on the same page then you should use locators with a different scheme.

From the above Screenshot, you can find the element using the locator: Name


3. Link Text: To Select The link element which contains the matching text.

driver.findElement(By.linkText(“Create Account”));

4. Partial Link Text: To Select The link element which contains the matching text.

driver.findElement(By.partialLinkText(“Create Account”));

5. Xpath: XPath is the language used when locating XML (Extensible Markup Language) nodes. Since HTML can be thought of as an implementation of XML, we can also use XPath in locating HTML elements.

Advantage: It can access almost any element, even those without class, name, or id attributes.

Fortunately, Firebug can automatically generate XPath locators. In the following example, we will access an image that cannot possibly be accessed through the methods we discussed earlier.

We can classify XPaths in the following two groups.

  • Absolute XPath: 

It starts from the root element within the web page or part of the page and goes to identify the target element.

Path start with the node of our choice


6. Tag Name: To Find The Element Using Its HTML Tag.

Select select = new Select(driver.findElement(By.tagname(“select”)));


7. ClassName: The CSS class locator uses a specific class attribute to get to the first element on a web page. It is useful for items that own a unique style.


8. Css Locator:

CSS Selectors are no different than the XPaths. But they are resilient and powerful. Unlike the XPath, they aren’t dependent on the DOM structure. They can help you perform actions which are difficult to do with XPath.


Leave a Reply

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