Different ways of finding elements using XPath

Bruno Macedo
4 min readDec 15, 2022

--

Demo site for this article

If you are using Selenium and you can’t find an element through the usual locators like, ID, CSSSELECTOR, NAME etc, the best way of finding elements is through XPath.

Just open dev tools (right click on the page and then “Inspect” o just press “F12” on your keyboard) and check the HTML.

Inspecting page to find element selector

Absolute XPath

To find the absolute XPath just right click on the element you want to inspect, then Copy and Copy XPath. Then paste it in your code.

Copying the Absolute XPath of the element

This XPath can be quite long and it might slow down your automation. There are alternative options to Absolute XPath.

Relative XPath

The basic format of the relative XPath is: “//tagName[@attribute=’value’]” . Inspect the element you want to interact with and find a unique identifier with <tag> and attribute, as below. I want to find the “Username” textbox, so I right click on it and inspect.

You can see that the element is an “input” tag and that has an attribute “name” and the value of this attribute is “user”. Simply use the basic format given above and check if the element with that element and attribute value is unique in the page. To do that, just press CRTL + F in the dev tools and you will see a search box at the bottom of the page. That’s where you’re going to write the XPath and check if it’s unique.

The element will be highlighted (in this case, in green) and there’s only one match for this locator as stated in the search box.

Using the method “contains()”

Another way of finding XPath is by using the method contains(). We usually choose this method when the value of the attribute changes dynamically. You can use just partial text to find elements in the page. Note that the password textbox has a “type” attribute with the value “password”. We can use a partial value to identify that element.

Using the “and” expression

You can also find the element in the page using the “AND” expression, where you present two conditions and both should be true to find the element.

Using the “text” expression

There is also a function that can be used to find elements in the page. If this element has a text in it you can use the expression text().

Using index

This approach is useful when you want to specify a tag using the index value of the element you wish to locate. You can see that under the FORM tag there are 5 INPUT’s.

If you want to input a text in the 2nd INPUT tag, which is the last name textbox, finding the element using its index is a good approach. First you find the parent tag, which is FORM, and then you find the tag that has index, INPUT. You then have to put them in parenthesis and then specify which index you want into brackets.

If you seek more information about it, you can watch my YouTube video on this subject below.

https://www.youtube.com/watch?v=OImnRRkwTHo&t=83s

--

--

Bruno Macedo
Bruno Macedo

No responses yet