Hey Guys, 

“XPath for beginners” articles are authored by our guest Megha Kharbanda. These awesome tutorials will help you to understand xpath from scratch.  

Part 1: About x-path and its basic syntax:

X-Path is used to navigate through web elements and attributes in an XML Document. From automation testing perspective (especially with selenium), xpath helps in identifying the web elements.

X-Paths can be created by:

  • Basic Syntax
  • Using AND & OR
  • Contains ()
  • Starts – With ()
  • Text ()
  • Following
  • Preceding
  • Ancestor


Basic x-path:

// : specifies that you want to search specific tag
Tagname: tag you are looking for
Attribute :  attribute in that tag
Value : value of that attribute
Example with a text field:
Consider a text field “Username”: Attributes of Username is:

The xpath of this field will be //input [@id = ‘username’]. Here @id will turn your focus on textbox
In case you do not have ID of the object:

EXAMPLE:  Attributes of a login button: 

xpath will be: //input [@value=’Login’]

Another example of xpath for image:

HTML Attributes of image is: 
< a title = ‘Powered by W’ href = ‘’http://www.wword.com’’/>
xpath will be: //a [@title = ‘Powered by W’]
If any attribute is not sufficient to identify a web element, you can take help of another element.
SYNTAX:  //tagname[@attribute1=’value1′] [@attribute2=’value2′]
Example: Attributes of a password field:

Since class is same for all textboxes on that page , so it is not sufficient to identify , therefore we take another attribute.
xpath will be: //input [@class = ‘input’] [@name = ‘password’]
It can also be used in this way:
Using OR in xpath//input [@class = ‘input’ OR @name = ‘password’]
If any of the above can be true then it will identify the web element.

Using AND in xpath: //input [@class = ‘input’ AND @name = ‘password’]
Both of them has to be true in order to identify the web element.
In Part 2, we will explore advanced methods of xpaths.
Happy Testing,