Selenium WebDriver Locators
Selenium WebDriver Locators will discuss about the different kinds of locators which are available in the selenium webdriver. The basic use of locator is to identify the element on the web page uniquely. All the elements which are there on the webpage are called WebElements in selenium webdriver.
Below are the webdriver supported locators:
- ID
- ClassName
- TagName
- Name
- LinkText
- Partial LinkText
- CSS Selector
- XPath
Now we will discuss all these things in detail.
ID:
This is the most efficient and preferred way to locate an element. This is one of the attributes to the tags which are used to develop the application. The concept of putting ID to an element is to identify the element uniquely. But developers may put or may not put the IDs to the tags. If we use IDs to locate the elements, then the identifying the element is accurate and fast.
Sample HTML Code:
<input id="datepicker2" class="is-datepick" type="text"/>
The above code will indicate a textbox on the web page. If I want to interact with this text box we can use the below code.
Selenium Code:
driver.findElement(By.id("datepicker2"));
ClassName:
This is also one of the attributes to the tags which are used to develop the application. But the basic motto of using a class is to apply some style to the element. There is a chance of using the same class name to different type of elements to apply the same style. So, this attribute is not unique to identify the elements. But combination of classes can be used to identify the element.
Sample HTML Code:
<input id="datepicker2" class="is-datepick" type="text"/>
The above code will indicate a textbox on the web page. If I want to interact with this text box we can use the below code.
Selenium Code:
driver.findElement(By.className("is-datepick"));
TagName:
By using tagName also we can identify the elements in selenium webdriver. But there is a chance of having more elements with the same tag name. So it is very difficult to identify a single element using tag name. But if you want to identify more than an element which is having the same tag name then we can go with tag name.
Sample HTML Code:
<input id="datepicker2" class="is-datepick" type="text"/>
Selenium Code:
driver.findElements(By.tagName("input"));
Here it might return more than one element as there is a chance of more ‘input’ elements on the page. It is not a good practice to identify single element using tagName always. It is very helpful while finding more elements.
Name:
This is also one of the attributes to the tags which are used to develop the application. But this is not mandatory to any tag. This will give just an extra information about the tag.
Sample HTML Code:
<input id="datepicker2" name="enableddatepicker" type="text"/>
Selenium Code:
driver.findElements(By.name("enableddatepicker"));
LinkText:
This is also one of the ways to identify the element. But by using this locator we can not locate all kind of elements. By using this we can identify only the links available on the web page. A link is an anchor tag on a web page. Here we need to use all the link text to identify the element.
Sample HTML Code:
<a href="Register.html">Register</a>
The above code will indicate a link on the web page. If I want to click/interact that link we can use the below code.
Selenium Code:
driver.findElement(By.linkText("Register"));
Partial LinkText:
This is an extension to the previous one. If you are not sure of the entire link text or want to use only part of the link text, you can use this locating mechanism to identify the link element. If more than an element containing the part of the text equal then you can identify more elements using this locator like tagName.
Sample HTML Code:
<a href="Register.html">Register</a>
The above code will indicate a link on the web page. If I want to click/interact that link we can use the below code.
Selenium Code:
driver.findElement(By.partialLinkText("Regi"));
XPath:
XPath (XML Path Language), is a query language for selecting nodes from an XML document. The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria.
XML contain one or more elements/tags/nodes, an element may contain content/data or may contain other elements as child nodes. Each element should have starting tag and ending tag. The element content will reside in between these two tags. Apart from the content each tag may have attributes. These attributes will give extra information about the element. We can locate any kind of element using XPath.
Can find more information at XPath in Selenium WebDriver
CSS Selector:
This is also similar to XPath. Here also we will use different kind of ways to identify an element. But this is slightly faster than the XPath. We can locate any kind of element using Css Selector.
Can find more information at CSS Selector in Selenium WebDriver
Please watch the YouTube video for this blog for better understanding.