0% found this document useful (0 votes)
5 views

DOMXPath

XPath is a language used to navigate and find elements on a web page using XML path expressions. It includes two types: Absolute XPath, which starts from the root node, and Relative XPath, which begins from the middle of the HTML structure. Various methods to write dynamic XPath expressions include using functions like contains(), starts-with(), and axes methods to locate elements based on attributes and relationships in the XML document.

Uploaded by

aparnanair.asn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DOMXPath

XPath is a language used to navigate and find elements on a web page using XML path expressions. It includes two types: Absolute XPath, which starts from the root node, and Relative XPath, which begins from the middle of the HTML structure. Various methods to write dynamic XPath expressions include using functions like contains(), starts-with(), and axes methods to locate elements based on attributes and relationships in the XML document.

Uploaded by

aparnanair.asn
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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’]

• // : Select current node.


• Tagname: Tagname of the particular node.
• @: Select attribute.
• Attribute: Attribute name of the node.
• Value: Value of the attribute.
Types of XPath

 1) Absolute Xpath

 2) Relative XPath
Absolute XPath

 It is the direct way to find the element


 It begins with the single forward slash(/)
 We can select the element from the root node.
 Eg:

 /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

 It starts from the middle of HTML DOM structure


 It starts with double forward slash (//)
 Always preferred as it is not a complete path from the root element.
 Eg:

 Relative XPath: //div[@class='featured-box cloumnsize1’]


 //button[@aria-label='Search']
Methods to Write Dynamic 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()

 Used when the value of any attribute changes dynamically.


 The contains() feature has an ability to find the element with partial text.
 Eg:
 Xpath= //*[contains(@type,'sub')]
( In this XPath expression partial value ‘sub’ is used in place of submit
button.)

 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()

 This method is used to locate elements based on text of a web element.


 The elements to be located should be in string form.
 Eg:
 Xpath= //td[text()=“UserID”]
 Xpath= //span[text()=“Conrad Bengaluru”]
Xpath Axes Method

 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

You might also like