Now we have had a quick look at what Xpath is and how it is used, let’s look at some expressions you can use for user journey selectors.
Basic Xpath
//a[@title=”Register / Log In”]
We briefly covered this earlier. Basic Xpath simply uses the tag name, attribute and value. This is the most basic of expressions. Multiple expressions can follow each other however there are better selectors which can be used such as ‘Follow’, ‘Ancestor’ and ‘Child’ which we will cover in the next lesson.
Contains
//a[contains(@title=”Register / Log In”]
The ‘Contains’ expression is very similar to basic Xpath however a full value isn’t needed. This is perfect for selecting part of a class or in the case of part of a value changing on a refresh.
OR & AND:
//a[@type=’submit’ or @name=’btnReset’]
The ‘OR & AND’ expression is slightly more flexible as it allows a selector to look for multiple conditions rather than just one. Although this can be used for many uses, a good example would be minor A/B testing for example to click a product with attributes/values that differ.
Xpath Starts-with
//a[starts-with(@id,’message’)]
‘Starts’with’ is very similar to contains however the selector is only looking for the beginning of the value. This is also great for finding an element whose attribute value changes on a refresh.
XPath Text
//a[text()=‘Login’]
Xpath text expressions use text on the webpage. Text is extremely useful on pages that are almost completely dynamic elements but can also be used if attribute values aren’t unique or just because it is easier.
It is worth noting that there is no right or wrong way at using Xpath selectors and any of these expressions can be used depending on your preferences. Creating journeys can be a trial-and-error process so you might find that different expressions fit best after running a few save & checks.