Sunday, 1 May 2016

What is Xpath and how to write Xpath ?

In my previous post I have explained how to get HTML code for particular web element and view the tags, attributes and values.In this tutorial we will learn that how to write xpath to uniquely identify web elements so that we can perform actions like click , type and double click on them.

Xpath is w3c recommendation which is used to traverse XML document by using path expression. We are going go see simple examples of writing Xpath which will be useful for us in writing selenium automation test cases.

If you observe the HTML code then you will notice that for each tag there are attributes and values for it. We need to use these attributes and values to write our xpath. HTML tags are like <div>, <input >, <a>, <span> etc. The attributes are like id, class,href,text etc. And each attribute will have value.So using this we can create our own combination of Xpaths.

Video Tutorial -


I will tell you most commonly used syntax in writing xpath. 
// - to refer Node/Tag
@ - to refer attribute
' ' - to specify value

If you know these 3 then half of your work is done.

Let us see some examples of Xpath 

1.  //input[@class='cname']

2. //span[@text='textvalue']

3. //div[@class='classvalue'][contains(@onclick,'text value')]

4. //div[contains(@class ,'cname') and text()='Hello Text']

These are few sample examples which are pretty much self explanatory. If you know the tag name, attribute and value then you can use them to write your xpath as shown the above example. If you have any doubts or suggestions feel free to drop a comment.


  1. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

  2. Nice blog, it’s so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job. Thank you for this wonderful sharing with us. Keep Sharing.
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata

  3. It's very useful article with inforamtive and insightful content and i had good experience with this information. We, at the CRS info solutions ,help candidates in acquiring certificates, master interview questions, and prepare brilliant resumes.Go through some helpful and rich content Salesforce Admin syllabus from learn in real time team. This Salesforce Development syllabus is 100% practical and highly worth reading. Recently i have gone through Salesforce Development syllabus and Salesforce Admin syllabus which includes Salesforce training in USA so practically designed.

  4. Gone through this wonderful coures called Salesforce Certification Training in Dallas who are offering fully practical course, who parent is Salesforce Training in USA and they have students at Salesforce Training classes in Canada institutes.

  5. You can join the free orientation at Workday training institutes in Bangalore and they located at Workday training institutes in delhi. It is highly recommendable for you to enroll at Workday training institutes in UK and you can have a great market for jobs in USA so join this wonderful Workday training institutes in USA and in India this Workday training institutes in Noida is really best institute.