Xpath
- Xpath is defined as XML path.
- It is a syntax or language for finding any element on the web page using XML path expression.
- Xpath is used to find the location of any element on webpage using HTML DOM structure.
- Xpath can be used to navigate through elements and attributes in DOM.
- Xpath is an address of the element.
Types of xpath
- Absolute / Full xpath
- ex: /html/body/nav/div/div[2]/ul[3]/li[1]/a
- /html/body/div[1]/div/div[3]/div[1]/img
- Relative / Partial xpath
- ex: //*[@id="header-navbar"]/ul[3]/li[1]/a
- //*[@id="divLogo"]/img
Difference between Absolute and Relative xpaths
- Absolute → xpath starts from root html node
Relative → xpath directly jump to element on DOM
- Absolute → xpath start with single slash “/”
Relative → xpath start with double slash “//”
- Absolute → xpath only use tags/nodes
Relative → xpath use attributes
How to write xpaths manually
/html/body/div[6]/div[1]/div[1]/div[2]/div[1]/ul/li[1]/a
syntax of writing relative xpath :
//tagname[@attribute=’value’]