This post was most recently updated on June 14th, 2019
Write Xpath using element text and string functions.
Information for this section
- Symbols used while Xpath creation
- ./ (period symbol and forward slash) → Current element
- /* (forward slash and asterisk symbol) → All child element of current element
- .. (double period symbol) → Immediate parent element of current element
- Functions without argument
- text() → Return text of element
- position() → Return position of element
- last() → Return last element in tree node structure
- Functions with arguments
- 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.
- 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]
- 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]
- Description: Create xpath to locate <a> element using partial element text.
Xpath: //a[contains(text(),’formation by builder asking owners’)]
Xpath using pipe symbol
Note: Locate web element using | (pipe) symbol.
-
- Write two or more xpath for same element
- Find two or more element’s by ORing their xpath (locate them based on availability).
- 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’]
- 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