Selenium Xpath Tutorial

Xpath is a Extendable Markup Language ,in selenium one of the element locator which is used to identify the web elements with path expression to navigate to web elements,this selenium xpath tutorial gives you complete real time examples to prepare proper xpath to identify the elements in a webpages.

Selenium Xpath Tutorial

Selenium Xpath Tutorial


Selenium Xpath Tutorial

Let's see xpath examples one by one ,as we know Firefox has a Firebug tool and WebDriver Element Locator tools to identify the xpath.in selenium xpath there are two types of paths is there i.e
  1. Relative Xpath
  2. Absolute Xpath

Relative Xpath

XPath use to select web elements in a webpage in case elements are not found with standard attribute name i.e id,name etc.In relative xpath we are using tagname,attribute name,attribute values to write the xpath in selenium.

Syntax

//tagname[@attribute name='attribute value']

Example

1:  //input[@id='firstname']  
2:  //input[@name='lastname']  
3:  //input[@id='email']  
4:  //a[@name='link']  


In above manner we can write the xpath and let's see in details how to write relative xpath in selenium for web elements.

Example

Suppose in a form we can see number of textboxes and those elements are created by <input> tagname as below
<input type="text" name="firstname" value="Rajesh">
For above <input> tagname you can write the xpath as below

//input[@name='firstname']

Here input is tagname ,@name is attribute name,firstname is attribute value ,like above exmaple you can write any relative xpath for every element.Let's see remaining types how to write xpath in selenium.

Using Contains

Contains also one more option in selenium to write the xpath for web elements ,see below explanation.

Example for Contains

1:  //input[contains(@name,'firstname')]  
2:  //input[contains(@id,'lastname')]  
3:  //input[contains(@id,'radiobutton')]  

2.Links

For links in a html where it is write under <a> tag ,we can also write selenium xpath for <a> tags as below

Example for links

Suppose <a> tag is used as 
<a href="http://softwaretestutorials.blogspot.in/p/selenium/">selenium</a>
//a[contains(@href,'selenium)]

3.LinkText

Identifying Selenium xpath for link text is very easy,just we have to know the link name i.e text for which link we need to identify in web page as below

Example for Linktext


//a[text()='selenium']/@href
//a[text()='loadrunner tutorials']/@href

2.Absolute Xpath

Absolute xpath in selenium useful in finding the web elements in webpage using in this way
/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[1]/div[3]/div/div[3]/div/input[1]

In Absolute path we are using "/" single backslash and writing the xpath from html tag to upto element location.

Post a comment

3 Comments

  1. Thanx....This is very useful tips for Selenium Testing people.

    ReplyDelete
    Replies
    1. Thank you for your valuable feedback and appreciation.

      Delete
  2. Need more information like independent and dependent concept

    ReplyDelete