Today we are covering the “Selenium Locators”,an important and very potent part of Selenium web driver.
What is a Selenium Web Driver?
Selenium WebDriver is a cluster of open source APIs which is used to automate the testing of a web application.
In simple words, it is used to check that if a particular web application works as it is meant to. It supports all the popular browsers such as Chrome,Safari,Edge etc.
Earlier we have to do this by ourselves and this was very hectic,but now it is very simple and efficient using Selenium.
Many components comprise Selenium and make it possible.One of them is “Selenium Locator”.
What is this Selenium Locator?
Selenium needs locators to match the elements of your web application with what it needs to run and use the process.
They are like the building blocks of Selenium Driver as without them Selenium would not be able to identify what it needs to interact with and what not.Locators ,if failed, can even result in script failure.
In other words, a good web application requires multiple elements found correctly. For this we have multiple locators listed below-
- Partial Linktest
- CSS Selector
Before jumping to what these do,we will see how to find a Document-object Model(DOM).
STEPS TO FIND DOM
2.Open the target application and click on F12 or right click and select inspect.
3. A section name as ‘Element’ would automatically open. This is where we hit on elements through. To the left most, a mouse icon can be observed. As you hover on the mouse icon, it would state ’select an element in the page to inspect it’. Click on it and steer to the element you want to locate to.
4.This is it, the selected row in the DOM is the context from where you need to fetch your values from.
This will look like this-
<a href=”https://accounts.example.com/register” target=”_blank”>Free Sign Up</a>
Now you can choose the tagname i.e ‘a’ and text i.e ‘Free Sign Up’ to locate your element.
➢ ID Locator
This marks the most common way to locate elements given they are unique to each element in the DOM
The World Wide Web Consortium(W3C) states that each element should be unique, making Id locator the most common and easy move.
But developers may or maynot follow this as many browsers allow bypassing this rule.
In those cases,we use other means to locate elements.
<object name>.findelement(By.Id(“value of the ID”).provide action which you want to perform.
➢ Name Locator
Just like how we use names to identify and locate Humans,Selenium also uses element name to locate them.
They might not be unique to a page,in that case Selenium selects the first element with that name on the page.
If no element by that name is found,then an expectation is raised.
➢ Link Text Locator
Elements can also be located by their links as in hyperlinks.In Case of multiple links for the same text,the first one would be selected.
➢ Partial Link Text Locator
This is what you can consider a slightly upgraded form of Link Text Locator.
If you have a very long link and you just want to use the partial text to locate the link, we can use this.
It can also be used to find multiple links with the same partial text.
➢ Tag Name Locator
As if it is not clear from the name itself , this is used to identify elements using CSS tags like div tag,a tag etc.
It can be used to check all the links to see if they are functional or not.It can also be used to see content within a tag.
➢ Class Name Locator
Class Name locator aids in locating elements defined through the class attribute.
Class Name locator accords the element which matches the values specified in the attribute name “class”.
➢ CSS Selector Locator
CSS Selector is the amalgamation of an element selector and a selector value that identifies the web element within a web page. The amalgamation of an element selector and selector value is known as Selector Pattern.
CSS or Cascading Style Sheets are used to style webpages and thus it becomes a way to locate elements.
CSS Selectors can be located in various formats as below-
=>Tag and ID =>Tag and Class =>Tag and Attribute
=>Tag, class and attribute =>Matches(starts with,ends with,contains) =>Child Elements
➢ XPath Locator In Selenium
Xpath or XML path is used to identify elements from XML documents.It has path expression along with some conditions.
To locate any element we just have to write a simple XML query/script.
Tagname signifies the tag in the DOM structure you are targeting to.
There are 2 types of Xpaths-
Difference between Absolute Xpath and Relative Xpath
Absolute XPath begins with the root node or a forward slash (/). The advantage of using absolute is, it recognizes the element rapidly.
The disadvantage is, if anything goes wrong like some other tag added or removed in between, then this path will no longer work.
Relative Xpath :- A relative xpath is one where the path starts from the node of your choice — it doesn’t need to start from the root node.It starts with Double Forward Slash(//).
The superiority of using relative xpath is, you don’t need to mention the long xpath, you can start from the middle or in between.
The disadvantage here is, it will take more time in identifying the element as we specify the partial path not (exact path).
Things to keep in mind while using locators
There are certain things to keep in mind while using these locators.Following these will ensure a smooth and error-free experience with these things.
Do not use locators over elements which may change making your locators failure-prone.
Locators should be dependent on one single element rather than multiple ones.Also make sure if you have dependent locators,then they should be less-likely to change.
Locators should only have required information collected.
If we are looking at multiple matches then it should have all the desired selections it needs to have.
Locators should not be used on auto-generated values.
These were the factors to consider while using Locators.They may sound complex for once but become easy if you use them while following the rules.