Listen to this blog
Object locating technique is one of the fundamental when it comes to automation testing. If we test the application manually, we would interact with various objects or elements like links, buttons, radio buttons, lists, text boxes, sliders, and so on. So for us to mimic an actual user, the automation tool would also have to perform the same actions, and locating the objects that have to interact with is the first goal. Once the object is located, we can perform the desired actions like clicking on it, entering text, and so on. Selenium is one of the prominent automation testing tools and it uses the concept of ‘Locators’ to identify the elements/objects. So let’s take a look at the Different XPath Methods in Selenium as they are the building blocks of any Selenium automation script.
Steps to find elements in DOM:-
Before we head over to explore the Different XPath Methods in Selenium, let’s also look into a few basics like the ways to find elements in DOM and the different locators that exist in Selenium.
1. First and foremost, you have to open the website that you want to automate. Once it has loaded, click on the ‘F12’ key to open DevTools. As shown in the below image, click on the inspect icon to enable it and then click on the element you want to inspect.
2. There are also other ways to open the developer’s tools. You can either click on the ‘Menu’ option in the browser, click on ‘More Tools’ under that, and then click on the ‘Developer tools’ option that is shown.
3. If not you can just right-click on the element you want to inspect and click on the ‘Inspect’ option to locate the element.
Different Types of Locators in Selenium:-
Now that we know the ways to open Developer tools in our browser, let’s take a look at the different types of locators that can be used to identify the various web elements.
- Partial LinkText
- CSS Selector
ID is the first choice for many testers as it is one of the safest and fastest locators that can be used to find the location of an element. What makes ID so effective is that it is unique for each element on the web page, making it easily identifiable. IDs are mostly account numbers, university roll numbers, usernames, passwords, and so on as they will be unique.
If you want to locate an element that doesn’t have too many elements with similar names, you could use the name locator as it is also an effective way to locate an element. If we take a form to be an example, you might have various text fields and one submit button. So locating text fields using the names can be hard. But since there is only one submit button you will be able to locate it without much confusion. If at all there are two or more elements with the same name attribute, then the first element with the value of the name attribute will be returned. If at all there are no elements that have a matching name attribute, then a NoSuchElementException will be raised.
Of the Different Types of Locators in Selenium, there are two locators that can be used to locate hyperlinks on a webpage. So let’s say you want to verify if a link is working correctly on a page. You can make use of linkText to locate that hyperlink by using the exact match of the string.
The main distinction from the LinkText in Selenium to the Partial LinkText is that it doesn’t investigate the specific match of the string. So it will even be able to identify fractional matches. This will come in handy when you have to search hyperlinks that have a high text length.
What if you have to locate complex elements on the page? The CSS selector will be the best choice for you. The only downside with CSS identification is that it is not supported by all browsers. But it may not be a huge concern as it is supported by all the widely used browsers like Google Chrome, Mozilla Firefox, Safari, Opera, and so on.
XPath stands for XML path language. It is based on the tree model of XML documents and provides the ability to navigate around the tree by selecting nodes using a variety of criteria. Though XPath locators are reliable, they are slower in comparison to the CSS Selectors. XPath is also supported by all the major web browsers. Now, let’s take a look at the different XPath methods and their syntaxes.
The Different XPath Methods in Selenium:-
basic xpath – To select a node based on the attribute,
//tagname[@attribute =' value']
contains – We can use partial text to find an element,
using OR – The element will be located if any one condition or both conditions are true.
//tagname[@attribute='value' or @attribute ='value']
using AND – The element will be located only if both the conditions are true,
//tagname[@attribute='value' and @attribute ='value']
starts-with – This can be used in a webpage where the attribute value changes on refreshing the page or due to dynamic operation on the page as it will locate the element with the starting text alone,
text() – It is an inbuilt function used to locate the element based on the text of the element,
axes – Can be used to locate elements that are more complex and dynamic by following various methods like following, parent, child, and so on.
Knowing the Different XPath Methods in Selenium is the first step, but employing them in an effective manner to get the job done is the key. As a leading automation testing company, we make use of some of the best locating techniques that have made our job easy. So let’s explore them now.
1) To find an element using the last and position keywords
//input[@type='text'][position()=1 or position()=last()]
We can get the first and last element inside a div tag using the position and last keywords.
2) To locate an element with the keyword count
We can locate an element by the count of another element. In this example, we have located the div tag by the count of its child input tags (i.e.) 2.
3) To identify an element with the parent keyword
Since we can locate an element with the element of the “parent” keyword, we have located the div tag which is the parent of the input tag.
4) To locate an element with the following- sibling keyword
In this example, we have three input tags under a parent tag div. If you want to locate only one input tag among the three tags, we can use the following-sibling keyword to do so.
5) To identify with a single class attribute
Consider a situation where there are 4 input tags and that one input tag has a class attribute. In this case, if all the other elements for the input tags are the same, we can locate the element with the help of the Class attribute.
6) Odd & Even Rows in a Table
We can even locate the elements in an odd and even row of a table by using the below commands. It will work like a filter as we will get all the elements in the even/odd rows as the result.
ODD ==> table[id=table1] td:nth-child(2n+1) Even ==> table[id=table1] td:nth-child(2n+2)
7) List of elements in a table
If you’re looking to locate a list of elements in a table by sorting them in ascending or descending orders, we can use the below technique.
8) The Indexing Technique
If you want to select an element inside the table, you can use this indexing technique to locate the particular element.
9) To locate elements based on the value
We have used an example where all the elements that have a price that is less than or equal to 2 are located.
//item[price >= 2]/price
10) To verify if a checkbox has been checked or not
Say you want to pick out elements that have not yet been checked in a list of elements, you can use the below technique to isolate the type of elements you want.
input#good:checked input#good:not(:checked) Here, input - tag name, good - id
We hope you found this blog to be informative as knowing the Different XPath Methods in Selenium is one of the must-know basics of automation testing. As a leading QA company, we are always keen on the knowledge and the positive mindset that is required to create swift solutions to big problems. So gaining an in-depth understanding of the various techniques will play an instrumental role in your growth as an automation test engineer. If you are not clear, then you will have a tough time figuring out quick solutions for your automation needs.