Home > Automation Testing > Write xpath using attribute and tag name in Selenium

Write xpath using attribute and tag name in Selenium

This post was most recently updated on June 14th, 2019

Xpath Basic Information

Xpath is a language and used to find targeted web element within html document.

Types of Xpath
Absolute Xpath:

  • Path from root element to targeted element without missing any elements in between.
  • Absolute xpath could be form by using ‘/’ (single forward slash) and ‘[index]’ (square bracket with index).
  • Example: To locate <input> element: “html/div/div[2]/form/div[3]/input[2]”

Relative Xpath: 

  • This xpath is specific to the target element. It uses expression to locate web element(s) in html document.
  • It uses “//” (double forward slah) and “/” along with other symbols, functions and axes name.
  • Example: To locate <a> elements in html document. “//a”

 

Xpath: Using Tag Name, Attribute and Logical Operators

Using Tag Name
Note: / denotes direct child of node while // denotes direct or indirect i.e all descendant elements of current node.

  • Description: Select all div tag in current open web page.
    Xpath: //div
  • Description: Select <a> tag which is direct child of div element
    Xpath: //div/a
  • Description: Select <a> tag which is direct or indirect child (i.e all descendant element) of <div> element
    Xpath: //div//a
  • Description: Select all those <div> element where <p> is direct child of <div> and <input> element is direct child of <p>.
    Xpath: //div[p[input]]
  • Description: Select all those <div> element where <p> is direct child of <div> and <input> element is direct or indirect child (i.e all descendant element) of <p>.
    Xpath: //div[p[//input]]

 

Using Tag Name and Attribute

Note: attribute::ATTRIBUTE_NAME is similar to @ATTRIBUTE_NAME.

  • Description: Select all input tag which contains name attribute.
    Xpath: //input[@name]
    Or
    Xpath: //input[attribute::name]
  • Description: Select all input tag which contains name attribute with attribute value “username”.
    Xpath: //input[@name=’username’]
    Or
    Xpath: //input[attribute::name=’username’]
  • Description: Select input element form div element with class attribute.
    Xpath: //div[@class=’class01 class02′]/input[@name=’username’]
    Or
    Xpath: //div[attribute::class=’class01 class02′]/input[attribute::name=’username’]

 

Using and, or and not along with Tag Name and Attribute

  • Description: Select all <input> elements which contains name and placeholder attribute.
    Xpath: //input[@name and @placeholder]
    Or
    Xpath: //input[attribute::name and attribute::placeholder]
  • Description: Select all input tag which contains name attribute with attribute value “username”.
    Xpath: //input[@name=’username’ and @type=’hidden’]
    Or
    Xpath: //input[attribute::name=’username’ and type!=’hidden’]
  • Description: Select input tag such that,
    a) It is direct child of div element
    b) It’s name attribute value is “tabName”
    c) It should not have placeholder attribute.
    d) Type attribute of input element should not be equal to “hidden”.
    Xpath: //div/input[@name=’tabName’ and not(@placeholder) and @type!=’hidden’]
    Or
    Xpath: //input[attribute::name=’tabName’ and attribute::type!=’hidden’ and not(attribute::placeholder)]
  • Description: Select <a> elements such that,
    a) <a> is direct or indirect child of <div> element.
    b) Href attribute value “htttps://www.google.co.us” or class value “socialApp”
    Xpath: //div/a[@href=’htttps://www.google.co.us’ or @class=’socialApp’]
    Or
    Xpath: //div/a[attribute::href=’htttps://www.google.co.us’ or attribute::class=’socialApp’]

Continue with Part 2

Selenium Tutorial >>

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Avatar photo
Neeraj Vishwakarma
Senior QA Engineer Skills: Automation Testing, Manual Testing, STLC, Java, Selenium Webdriver, Protractor, Database Testing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">