DOMXPath
DOMXPath
BY NAVEEN JOHN
Xpath :
It is a syntax or language for finding any element on a web page using XML path expression.
XML path used for navigation through the HTML structure of the page.
The basic format of Xpath :
//tagname[@attribute=‘value’]
1) Absolute Xpath
2) Relative XPath
Absolute XPath
/html/body/div[2]/div[1]/div/h4[1]/b/html[1]/body[1]/div[2]/div[1]
But if there are any changes made in the path of the element then that XPath gets failed.
Relative XPath
1. Basic Xpath
2. Contains()
3. OR & AND
4. Xpath starts-with
5. Text() function
6. Xpath Axes method
1. Basic Xpath Method
XPath expression select nodes or list of nodes on the basis of attributes like ID , Name,
Classname, etc. from the XML document
Eg :
Xpath= //input[@type='text’]
Xpath= //label[@id='message23’]
Xpath= //input[@value='RESET’]
Xpath= //a[@class='barone']
2. Contains()
Xpath= //*[contains(text(),”here”)]
Xpath= //button[contains(@data-testid,'search-b')]
OR & AND Method
In OR expression, two conditions are used, it is also applicable if any one condition is true or maybe both are
true.
Eg:
Xpath=//*[@type='submit' or @name='btnReset’]
(In this XPath expression, it identifies the elements whose single or both conditions are true.)
Xpath= //button[@data-testid='search-button' or aria-label="Search"]
In AND expression, two conditions are used, both conditions should be true to find the element. It fails to find
element if any one condition is false
Eg:
Xpath=//input[@type='submit' and @name='btnLogin']
Xpath Starts-With()
In this method, the starting text of the attribute is matched to find the element whose
attribute value changes dynamically.
Eg:
Xpath=//label[starts-with(@id,'message’)]
Xpath= //label[starts-with(@for,'accomodation')]
Xpath Text()
XPath Axes are the methods used to find dynamic elements, which otherwise not possible by normal XPath
method.
XPath axes search different nodes in XML document from current context node.
Commonly used axes methods :
Child
Parent
Ancestor
Sibling
Preceding
Self etc..
Xpath Axes Method
Self
Selects the current node or ‘self’ means it indicates the node itself
It always finds only one node as it represents self-element.
Xpath =//*[@type='password']/self::input
Following-sibling:
Select the following siblings of the context node. Siblings are at the same level of the current node.
Xpath=//*[@type='submit']/following-sibling::input
Xpath= //li[@class="bg-white 2xl:rounded-sm
DealsYouWillLove_columnListItem__lSUzy mx-1 flex-shrink-0"]/following-sibling::li Tri
Xpath Axes Method
Parent
Selects the parent of the current node.
Xpath=//*[@id='rt-feature']/parent::div
Xpath=//div[@class="invalid-tooltip py-2 px-3"]/parent::div
Child:
Selects all children elements of the current node
Xpath=//*[@id='java_technologies']/child::li
Xpath Axes Method
Descendant
Selects the descendants of the current node.
Xpath=//*[@id='rt-feature']/descendant::a
Xpath=//form[@method="post"]/descendant::div
Ancestor:
The ancestor axis selects all ancestors element (grandparent, parent, etc.) of the
current node.
Xpath=//*[text()='Enterprise Testing']/ancestor::div
Xpath Axes Method
Following:
Xpath=//*[@type='text’]/following::input
Selects all elements in the document of the current node
To focus on any particular element then we can use the below XPath method:
Xpath=//*[@type='text’]/following::input[@id=‘label’]
Preceding:
Select all nodes that come before the current node.
Xpath=//*[@type='submit’]/preceding::input