Create XPath
In Selenium web automation, if the elements are not found by the general
locators like name, id, tagname, class, etc. then we use XPath to find an element on the web page.
In this tutorial, we will learn about how to create different XPath expression to find the
complex or dynamic elements, whose attributes changes dynamically on refresh or any operations.
Topics to cover
- What is XPath?
- Types of X-path
- XPath Axes
- Create XPath using different ways
- By usign Attribute
- By usign Contains()
- By usign And & OR
- By usign Starts-with()
- By usign Text()
- By usign XPath Axes method 'following'
- By usign XPath Axes method 'following-sibling'
- By usign XPath Axes method 'Ancestor'
- By usign XPath Axes method 'Parent'
- By usign XPath Axes method 'Descendant'
- By usign XPath Axes method 'Child'
- Access child nodes without XPath axes methods
- Access parallel and parent nodes without XPath axes methods
What is 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 web/HTML element
on a webpage using HTML DOM structure. The basic format of XPath is explained below with example.
// tagname [ @ attribute = 'value']
- //: Select current HTML node.
- Tagname: Tagname of the particular HTML node.
- @:Uses to select attribute.
- Attribute: Attribute name of the HTML node. (We can select any attribute of HTML node to access web Element)
- Value: Value of the attribute.
Types of X-path
1) Absolute XPath
2) Relative XPath
Absolute XPath:
<html>
<body>
<div><b>Selenium Absolute XPath</b></div>
<div><p>paragraph text</p></div>
</body>
</html>
Relative xpath:
<html>
<body>
<div><b class="relative-class">Selenium Absolute XPath</b></div>
<div><p>paragraph text</p></div>
</body>
</html>
XPath Axes
Create XPath using different ways
By usign Attribute
<html>
<div>
<div class="class-attribute">Div element with Class attribute</div>
<div id="id-attribute">Div element with Id attribute</div>
<input type="type-attribute">Input element with type attribute</input>
<a href="href-attribute">Anchor element with href attribute</a>
</div>
</html>
//div[@id='id-attribute']
//input[@type='type-attribute']
//a[@href='href-attribute']
By usign Contains()
// tagname [ contains( @ attribute , 'contained value')]
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="typeAttribute">Input element with type attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</html>
//div[contains(@id,'idAttr')]
//input[contains(@type,'typeAttr')]
//a[contains(@href,'hrefAttr')]
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="typeAttribute">Input element with type attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
<div>
<div class="classAttributeSecond">Another Div element with Class attribute</div>
<div id="idAttribute">Another Div element with Id attribute</div>
</div>
</html>
By usign And & OR
// tagname [ @ attribute = 'value' and @ attribute = 'value']
// tagname [ @ attribute = 'value' or @ attribute = 'value']
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
<div>
<div class="classAttributeSecond">Another Div element with Class attribute</div>
<div id="idAttribute">Another Div element with Id attribute</div>
</div>
</html>
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
<div>
<div class="classAttributeSecond">Another Div element with Class attribute</div>
<div id="idAttribute">Another Div element with Id attribute</div>
<input type="text" name="textName">Input element with type and name attribute</input>
</div>
</html>
By usign Starts-with()
// tagname [ starts-with( @ attribute , 'contained value')]
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="typeAttribute">Input element with type attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</html>
By usign Text()
// tagname [ text() = 'text value')]
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="typeAttribute">Input element with type attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</html>
By usign XPath Axes method 'following'
// tagname [ @ attribute = 'value'] //following::tagName
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
<div>
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
<div>[1]
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>[2]
</div>
<div id="idAttribute">Div element with Id attribute</div>[3]
</html>
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
<div>[1]
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>[2]
</div>
<div id="idAttribute">Div element with Id attribute</div>[3]
</html>
By usign XPath Axes method 'following-sibling'
// tagname [ @ attribute = 'value'] //following-sibling::tagName
<html>
<div>
<div class="classAttribute">Div element with Class attribute</div>[1]
<div id="idAttribute">Div element with Id attribute</div>[2]
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
<div>
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>
By usign XPath Axes method 'Ancestor'
// tagname [ @ attribute = 'value'] //ancestor::tagName
<html>
<div>[2]
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
<div>[1]
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>
By usign XPath Axes method 'Parent'
// tagname [ @ attribute = 'value'] //parent::tagName
<html>
<div>
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
<div id="parentIdAttribute">[1]
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>
By usign XPath Axes method 'Descendant'
// tagname [ @ attribute = 'value'] //descendant::tagName
<html>
<div class="descendant-class">
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>[1]
<div id="parentIdAttribute">[2]
<div id="idAttribute">Div element with Id attribute</div>[3]
<div id="idAttribute2">Div element with Id attribute2</div>[4]
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
</html>
By usign XPath Axes method 'Child'
// tagname [ @ attribute = 'value'] /child::tagName
<html>
<div>
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
<div id="parentIdAttribute">
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>
Access Child/Nested Child nodes without XPath axes methods
// tagname [ @ attribute = 'value'] /tagName
<html>
<div id="topParentId">
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
<div id="parentIdAttribute">
<div id="idAttribute">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>
<html>
<div id="topParentId">
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>[1]
<div id="parentIdAttribute">[2]
<div id="idAttribute2">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>[3]
</html>
or
//div[@id='topParentId']/div[1]
<html>
<div id="topParentId">
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>[1]
<div id="parentIdAttribute">[2]
<div id="idAttribute2">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>[3]
</html>
Access parallel and parent nodes without XPath axes methods
// tagname [ @ attribute = 'value'] /..
// tagname [ @ attribute = 'value'] /../tagName
<html>
<div id="topParentId">
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
<div id="parentIdAttribute">
<div id="idAttribute2">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>
<html>
<div id="topParentId">
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
<div id="parentIdAttribute">
<div id="idAttribute2">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>
or
//input[@type='submit']/../div[@id='idAttribute2']
<html>
<div id="topParentId">
<a href="hrefAttribute">Anchor element with href attribute</a>
<div id="idAttribute">Div element with Id attribute</div>
<div id="parentIdAttribute">
<div id="idAttribute2">Div element with Id attribute</div>
<input type="submit" name="name">Input element with type and name attribute</input>
<a href="hrefAttribute">Anchor element with href attribute</a>
</div>
</div>
<div id="idAttribute">Div element with Id attribute</div>
</html>