How to Use Absolute and Relative XPath Expressions#
An XPath expression can be written in two ways:
- Absolute: Starts with
/
- Relative: Starts with
//
For the examples below, let's imagine a simple web page:
HTML | |
---|---|
Absolute XPath#
To target the <h1>
headline element with an absolute XPath expression, use this:
This is also called a full XPath. Most web pages are much more complicated than this, e.g. with containers and other nested elements, and so you end up typing cumbersome, long XPath expressions. Most often you want to avoid absolute XPath expressions and use the relative variation instead.
Relative XPath#
With a relative XPath expression, you can target an id
attribute or other anchor points to simplify the expression. This also makes it more readable:
Or even simpler:
This way, the relative XPath simply searches for all child elements of the root node that matches the condition.
Tip: Use *
as Wildcard Selector
While //div[@id='container']
targets a <div>
element with a specific id
, it's often favourable to use a generic selector. Try using the asterisk *
in //*[@id='container']
instead. This is a wildcard selector that targets all element types whether it's a <div>
, <h1>
, or any other tag.
Multiple Conditions in One Expression#
Let's imagine a more complicated page with several nested children to <div id="container">
where we want to click the <a>
link element:
HTML | |
---|---|
How do we target the <a>
link element efficiently?
We can simply use multiple relative statements similar to this pattern //…//…
in one expression: