Selenium WebDriver: Locators in Selenium

Working with Locators

Every webpage or website comprises of several HTML and CSS elements, which constitute the DOM structure. These elements in selenium are known as WebElements and WebDriver provides us with various feature to locate any WebElement uniquely in our AUT. These WebElements are also referred as locators in selenium.

findElement(By) and findElements(By) are two of the WebDriver methods which are used to locate any WebElement. findElement() returns exact match if the WebElement is found in DOM, whereas latter returns a list of WebElements matching the locator type. By is the strategy employed by WebDriver to identify a locator. Following are the various By strategies using which we can identify a WebElement.

By id

id is the most widely used strategy, given that it is uniquely identified in AUT. This is the fastest technique to locate any WebElement.

Example how to use id to locate a WebElement:

<div id="myID">...</div>
WebElement element = driver.findElement("myID"));

Here element is a variable of type WebElement(every object in a webpage is considered as WebElement in Selenium), which is used to hold the object returned by findElement.

A driver is an instance of WebDriver, which is used to drive the browser and finally is the strategy with which we located a WebElement with id value myID.

By className

<div class="automation"> <span>QTP</span></div>
<div class="automation"> <span>Selenium</span></div>

In the above example, the class name of 2 WebElements is same “automation”. If we use By.className strategy then it will return us 2 objects of “automation”. To handle multiple objects we will use findElements method.

List<WebElement> elements = driver.findElements(By.className(“automation”));


By tagName

Every HTML has a tag associated with it. We can use this tag to locate our WebElement.

<a src= “” ></a>

Here “a” tag signifies the anchor tag, which represents a link in a webpage. A website may have multiple links in it, hence we will again use findElements method to extract multiple anchor tag objects and store them in a List of type WebElement.


List<WebElement> anchorTag = driver.findElements(By.tagName(“a”));


By name

We can use this strategy if it identifies a WebElement uniquely.

<input name="automation" type="text"/>

WebElement nameLocator = driver.findElement(“automation”));


How To Create xPath

click here

By linkText

We can locate a link using the text it displays.

<a href="">automation</a>

WebElement linkText = driver.findElement(By.linkText(“automation”));


By partialLinkText

This is an extension of the above strategy. We will use this technique if our link text is very long and we do not want to enter full link text. partialLinkText will return all the WebElements which matches the text of the link.

<a href="">automation using Selenium</a>

WebElement linkText = driver.findElement(By.partialLinkText(“automation”));


There are two other popular ways to identify a locator:

xpath is surely the best technique to locate a web element but it is relatively slow as compared to cssSelector.

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

1 thought on “Selenium WebDriver: Locators in Selenium

Leave a Reply

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