Locators provide a way to access the HTML elements from a web page. In Selenium, we can use locators to perform actions on the text boxes, links, checkboxes, and other web elements.
They 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.
However, it gets tough at times to accurately identify a web UI element. And, we end up working with the wrong elements or unable to find them. Hence, we’ll take you to the different methods to use locators with Selenium.
How to Use Locators in Selenium?
Selenium names eight types of locators to find the elements on a web page. Check out the below list of locators approved by Selenium.
We are explaining each of them one by one and with examples. You’ll find the list of locators in the best to least preferred order.
ID to Select the Element
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.
<input id="user" class="required" type="text"> WebElement item = driver.findElement(By.id("user"));
It is preferable to have a unique identifier, so it is unlikely to meet similar values.
Feasible for elements with fixed IDs but not for the generated ones.
Name to Select the Matching Element
Every form has input fields with unique names. Names are unique most of the time, but it’s not a restriction. 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. Let’s see the example where you can use either the id or the field name.
<input id="user" name="admin" class="required" type="text"> WebElement locator = driver.findElement(By.name("admin"));
It is more appropriate to use it when you have a list of similar types of elements.
Using it with a dynamically generated list is tough.
Link Text to Select the Link Element
It is a perfect way to find the links on a page.
<a href="http://www.google.com">How to use locators?</a> WebElement item = driver.findElement(By.linkText("How to use locators?"));
- It’ll only work for anchor tags.
- Use it for checking navigation flows.
You need to provide the link text for it to work.
Partial Link Text to Select Link Element
It is almost similar to the previous locator. It differs in the way you use it to find the element.
<a href="http://www.google.com">How to use locators?</a> WebElement item = driver.findElement(By.PartialLinkText("How to use locators?"));
Tag Name to Find the Element
You can better understand how to use this locator from the below example.
List<WebElement> linkElements = driver.findElements(By.tagName("results")); String linkTexts = new String[linkElements.size()];
CSS Class Name to Access the Elements
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.
CSS class locator example: WebElement element =driver.findElement(By.className(“sample”));
CSS Selector to Access the Elements
CSS Selectors are no different than the XPaths. But they are resilient and robust. Unlike the XPath, they aren’t dependent on the DOM structure. They can help you perform actions that are difficult to do with XPath.
CSS Selector example: WebElement CheckElements = driver.findElements(By.cssSelector("input[id=email']"));
- Relatively speedier than using the XPath.
- Its usage is growing as the web pages are getting more style-centric.
- It’s easy to define a unique CSS locator as you can combine multiple CSS attributes.
XPath to Locate Elements
It is a perfect technique for walking through the DOM structure of the web page. XPath locators are robust and reliable. It is one method that guarantees to locate any element on the page using the XPath expression. But you should be very careful while forming an XPath as it may not work if there are changes in the web application.
We can classify XPaths into the following two groups.
It starts from the root element within the web page or part of the page and goes to identify the target element.
Absolute XPath Example: HTML/head/body/table/tr/td
To use locators like the XPath is easy as you give the direct element path. But the XPath would break when the element structure changes.
The relative XPath are easy to manage as they are short and concise. It is also better than the previous XPath style as it may survive the changes in the Page HTML to a certain degree. However, building a relative XPath is time-consuming and quite tricky as you need to check all the nodes to form the path.
Relative XPath Example: //table/tr/td
Guarantees to find accurate locators.
- It is slow as compared to CSS.
- It’s browser-dependent, and there are differences in IE vs. Firefox XPath implementations.
Final Word – How to Use Locators in Selenium
It is our effort to share every bit we know about Selenium so that you can use it successfully in your live projects. We hope that this Selenium tutorial will help you in doing that. And you would be able to use locators in Selenium projects more efficiently than ever before.
Connect on our social media handles to receive free access to all of our future resources.
All the Best,