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.
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.
However, it gets tough at times to accurately identify a web UI element. And, we end up working with wrong elements or unable to find them.
Hence, we’ll walk you through the different methods to use locators for efficient web UI automation.
In this post, we are covering the Selenium locators in detail.
We’ve divided the post between two sections to bring more clarity on the topic.
- How to use locators in Selenium?
- What is the best locator strategy?
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.
We have sorted the list of locators in the best to least preferred order.
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 id, so it is unlikely to meet similar values.
Feasible for elements with fixed IDs but not for the generated ones.
Every form has input fields with unique names. Names are unique most of the times, 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.
It is a perfect way to find the links on a page.
<a href="http://www.techbeamers.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.
4- Partial Link Text to select link (anchor tag) element which contains text matching the specified partial link text.
It is almost similar to the previous locator. It differs in the way you use it to find the element.
<a href="http://www.techbeamers.com">How to use locators?</a> WebElement item = driver.findElement(By.PartialLinkText("How to use locators?"));
You can better understand to use this locator from the below example.
List<WebElement> linkElements = driver.findElements(By.tagName("results")); String linkTexts = new String[linkElements.size()];
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 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.
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 is a perfect technique for walking through the DOM structure of the web page. XPath locators are robust and reliable. It is one method which 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 in 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. Though, building a relative XPath is time-consuming and quite difficult 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.
It is our efforts to share every bit we know about the Selenium so that you can use it successfully in your live projects. We hope that this Selenium tutorial would have helped you in doing that. And you would be able to use locators in Selenium projects more efficiently than ever before.
All the Best,