what is xpath in selenium
Click to rate this post!
[Total: 0 Average: 0]

In selenium testing tool, if we do not found locators like name, character, ID. Then the Xpath is utilized to establish the component on the web page. In this blog what is XPath in selenium by CSS. You know the best ways in which Xpath used to establish the Critical and Dynamic elements. Where the attributes modifies dynamically on any other operations.

what is xpath in selenium

What is XPath in Selenium?

Generally, Xpath is nothing but a XML path, a language or syntax used to get aspects on a website, with the help of XML expression. The below is the syntax and know it by IT training

Syntax: Xpath=//tagname[@attribute=’value’]

For finding the elements on, web page we need Xpath locators.

CSS Path, CSS establishes basis have no ID, no character, and no name.

Xpath, it require searching the dynamic aspects and transverse in between the fundamentals of the web.

Link Text, for searching the basics by words of the URL.

Name, this locator used to catch the aspect by name of the fundamentals.

Class name, used to search the basics by section name of the basics.

ID, this locators used to catch the basics by ID of the essentials.

Locating Web Elements in Selenium

Generally Using exact methods for locating basis on a page considered. The main Skill of the test Automation Engineer with a selenium web driver. When we open a web page, we see the best part in that website. In particular, this is just like, a notification showed on a web. Alternatively, by clicking on submit button. Here a web driver produce many methods to Map basis on a website.

Without doing anything in the web page, there is no possibility to be communicate with the web page. To enumerate this designs the tricks and techniques to do a fast movement.

Search Context

A web driver Functions with Search content Interface for locating aspects on any web with IT certifications.

The search context Interface has only two methods.

Public Interface Searchcontext {

List FindElements(By by);

WebElement FindElement(By by);


Therefore, the web driver dashboard provides two type of methods. The first method will hunt the basis that exactly match an example. That what is recognized as a locator? To illustrate the Second method it recognizes the first unit that exactly match or it throws a No such Exception, when it not found.

Here the examine fundamentals method used for recognizing aspects, when you are not sure, similarly if they are on the page. Alternatively, if you make sure they are not on the page. Then what is a locator.

Locator shows what you are looking to catch on a web page, then we write

WebElement h1Element = Driver.findelement(By.tagname(“h1”));

Alternatively, if we need to catching total para graph fundamentals on a web page, we have to see

List pElements = Driver.findElements(By.tagName(“p”));
By Link text

In this method, it establishes characteristics by the words, what it show. This type of method is normally preferred for locator, for the Url’s on a web.

For Instance, suppose if we have this URL to a web. That is

<a href= “#” id=”change-password” class=“btn”>forgotten password</a>

Here the link is located by using:

Driver.findelement(By.linktext(“forgotten password”));
Partial link text

When cannot assume properly of exact wording of the URL letters, but need to examine URL that contains given letters, that we can use.




You have to be careful about using assets aspect with this locator, as there will be points. This has the same partial words. By this should not utilized to map a single point on its own. Equally It is the best usability to examine a group of characteristics using the examine essentials method.

With Class Attribute

This locate the modules by the value of the set attribute. Used only for those characteristics that have a set attribute. But it is not known as a good selector to utilize with Find element method.

If you are interested to learn selenium please go through selenium training

The class or the section attribute, by the essentials that have a class attribute. It is not a good selector for the usability with the findelement Method.

The class Attribute is utilized for using Style portals. Therefore, the options are that so many units are just like to have the similar class. As Defilement every time returns the first module it catch the feature, you requirement is not in first place. You won’t utilize this to map it.

Using the Similar example above the link, “forgotten password” link has one CSS class: btn that can use to map it.

<a id = “change-password” class= “btn” href = “#”>forgotten Password</a>

Then, the link can located by using:


It get element by ID the value of their ID attribute. Furthermore, the link in the above Instance has an ID that we use.

<a id= “change-password” class=”btn” href= “#”> forgotten password</a>

Then the link can located using:

Driver.findelement (By.id(“change-password”));

If ID is available, then it should use as the first preferred choice.

By name

It locates elements by the value of their name attribute. Normally it can use to get element by class, that built using: <input>, <button>, <select>, and <textarea>.

On a critical login portal, you have input fields, which could be just like.

<input section= “form-control” name=“email” type = “text” placeholder= “email”

We can locate email field input by name attribute

Driver.findelement(by.name (“email”));
By tag name

This locator searches the first principles by their HTML tag name. Since we have many repeating uses of many tags. It is not possible every time for using this method to locate a single aspect. However, it can combine with other type of locators for effectively mapping or locating basis.

<h1>welcome to SDET world!</h1>

After that, we locate the email field by Input name attribution.

Using CSS selectors

Especially Locating items by driver underlying W3 css selector engines. CSS selector locator is Powerful. For this reason in the same way, it used for locating any items on a web.

<a id=”change-password” class= “btn” href= “./Some-link”>forgotten password</a>

Simultaneously that we locate the Email field by input name attribution.


At the same time in the above, the #represents ID of the basics. As an illustration Notation, show represents the class attribute of basics.

For example

Solution by CSS selectors

Generally, it locates basis via the driver’s underlying W3 CSS selector engine. In particular, the CSS selector locator is very powerful. Most important it is used for locating any type of Basics.

Id= “change-password” class= “btn” href=” ./some-link”>Forgotten password</a>

In the mean time, most important we can locate the email field by Input name attribution.


In the above, the # represent ID of sector. However, the notation show the class css attribute selector of a sector.

For Instance
By Xpath

Especially Xpath locators known as common complex selector to use. It need knowledge in Xpath query language. So if you are not fluent in that query language. Finally, you examine it very difficult to examine basis by using Xpath Queries.

Now we will see an example of using Xpath for this type of HTML.

<a id=”change-password” href= “#”> change password </a>

As a result, we locate the email field by the input name attribution.

Driver.findelement(by.xpath(“//a[@id= ‘change-password’]”));

Drop Us A Query

100% Secure Payments. All major credit & debit cards accepted.

Call Now Button