We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

XPath in Selenium

XPath in Selenium

XPath is one of the Identifiers that can be used in Selenium to identify Objects on a Webpage. Though we have multiple identifiers, XPATH is more useful when you don’t have suitable Identifiers.

Identifier:

A Unique expression to Identify an Object in a Webpage, The List of Identifiers available are listed below

  • ID
  • Class Name

CSS Selector

  • Link Text
  • XPATH

 XPATH :

Assume you search a Webpage, based on your need. Once you search a page and hit enter. The page is rendered in your browser. The page is rendered in your Machine in the form of HTML DOM elements, which will be in the XML(Extensible Markup Language)

For E.g.:

<Button Name=”Besant”>
<Input Class=”ad-aeref”>

We can use expressions to navigate to the required element and then we can perform required action.

Though we can write an  Xpath expression its very important to write an Efficient and Reusable expression, which inturns will be very useful in terms of script maintenance, Consecutively providing less effort for script enhancement.

I hope you now know what exactly is XPATH. Is it that hard for you to understand the path still. We have many best and easy alternatives to write XPATH expressions

Let’s see those in the upcoming lines.

Upcoming Lines

So what is Absolute XPath and Relative Xpath:

Let’s dive into it with an Example:

<html>
<body>
<div>helloworld</div>
<body>
</html>

Above HTML Code would display  the below:
Output-1When we Inspect it through Developer tools of the launched browser,

Launched Browser

Xpath expression to reach “hello am besant” would be of the following:

Relative Xpath  ://div
Absolute Xpath : /html/body/div

Which would be the best?

Relative Xpath

Why?

Webpages are subject to change based on the business requirement. If the added any new feature to the webpage, the DOM elements would get changed which also affects the Absolute XPath for the required element. Also, Relative Xpath has more efficiency when compared to Absolute XPath. (as the Absolute XPath requires to travel node by node it would take more time compared to the Relative XPath).

What more in Relative Xpath?

Relative Xpath can be used with Axes which helps to make our expression more better and efficient.

Below is the list of XPath axes available :

  • Simple XPath
  • Text()
  • Contains()
  • Starts – with()
  • Follo wing (), Ancestor(), Descendant(),Following – sibling()

Simple Xpath :

Steps to writing a Simple Xpath:

Step 1: Starts with  –  //

Step 2: Followed by TagName –  //Input

Step 3: Followed by attribute name and value in Square brackets- as below

//Input[@name=”trial”]

Using text() :

It is used to look for a match with the given text(), mostly this function can be used with Contains() which will be more likely to be considered as effective.

For Example:

//input[contains(text(),”Trial2”)

Using Node Traverse functions:

As per the below image Assume you are in the self node, you can traverse through its parent and child, Ancestor and descendants using the following functions.

Node Traverse Function

 

For Example:

//input//following::input[0]

Now you would be able to use Xpath identifiers in your code to navigate the required object.

Related Blogs

Besant Technologies WhatsApp