Home > Automation Testing > Write xpath using text and text functions in selenium?

Write xpath using text and text functions in selenium?

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

Write Xpath using element text and string functions.

Information for this section 

  1. Symbols used while Xpath creation
    1. ./ (period symbol and forward slash)       Current element
    2. /* (forward slash and asterisk symbol)    All child element of current element
    3. .. (double period symbol)                           Immediate parent element of current element
  2. Functions without argument
    1. text()                                                       Return text of element
    2. position()                                                Return position of element
    3. last()                                                      Return last element in tree node structure
  3. Functions with arguments
    1. starts-with(arg1, arg2)  → where arg1 is text()/@AttributeName and arg2 is prefix string of arg1. It returns true if arg1 starts with text contains in arg2.
    2. contains(arg1, arg2)    → where arg1 is text()/@AttributeName and arg2 is partial string of arg1. It returns true if arg1 contains text in arg2.

Note: Used https://www.societyhive.com/ website for below examples and screen shot.

Xpath using text of child, symbols and functions

  • Description: Select <a> element which contains some text and it is direct child of <div> element.
    Xpath: //div/a[.=’Forgot your password?’]
    Or
    Xpath: //div/a[text()=’Forgot your password?’]

 

  • Description: Select <div> element which contains <a> as direct child and it has some text associated with it.
    Xpath: //div[a=’Forgot your password?’]

 

  • Description: Select immediate parent of <div> element which contains <a> as direct child and it has some text associated with it.
    Note: In case of immediate parent /parent::* or  /parent::div shows same element and  if <div> element is immediate parent otherwise no element will be found.
    Xpath: //div/a[text()=’Forgot your password?’]/parent::*
    Or
    Xpath: //div/a[text()=’Forgot your password?’]/parent::div
    Or
    Xpath: //div/a[text()=’Forgot your password?’]/..
    Or
    Xpath: //div/a[.=’Forgot your password?’]/..

 

  • Description: Select <a> element located at (N-1)th position in <div> element.

    Xpath: //div[@class=’footer_bottmlink’]/a[last()-1]
    Or
    Xpath: //div[@class=’footer_bottmlink’]/a[position()=last()-1]

    Xpath using position() and last() function
    Xpath using position() or last() function

     

  • Description: Class value in <div> element is dynamic value and class name starts with text ‘footer_’ on every time page loads. Create xpath to locate (N-1)th <a> element within that <div> element.

    Xpath: //div[starts-with(@class,’footer_’)]/a[last()-1]

    Using starts-with function
    Used start-with(@class, ‘attribute_start_string_’)

     

  • Description: Create xpath to locate <a> element using partial element text.

    Xpath: //a[contains(text(),’formation by builder asking owners’)]

    Used contains(text(), '_partial-text_')
    Used contains(text(), ‘_partial-text_’)

 

Xpath using pipe symbol

Note: Locate web element using | (pipe) symbol.

    1. Write two or more xpath for same element 
    2. Find  two or more element’s by ORing their xpath (locate  them based on availability).
    3. Find web element whose parent may not be same in whole web page
  • Description: Using | (pipe) symbol we can write two different xpath. It  is used for ORing between two different xpath for same element or for different elements. Here locate 3 <a> elements  using | pipe.

    Xpath: //a[.=’PRICING’] | //a[.=’HOME’] | //a[.=’FEATURES’]

    Use | (pipe) for xpath OR'ing
    Used | (pipe) symbol

     

  • Description: Find <input> element from <div> and <body> elements only. i.e all <input> element that are direct child of <div> and <body> element.

    Xpath: (//div | //body)/input

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="">