XPath can be used to navigate through elements and attributes in an XML document. It could be useful when data from the response cannot be extracted using the Regular Expression Extractor. For example, in the case of a scenario where you need to extract data from similar tags with the same attributes, but of different values. The XPath Extractor is similar to the CSS/JQuery Extractor but XPath Extractor should be used for XML content while CSS/JQuery Extractor should be used for HTML content. Let’s assume that in the response we have a table with different values where we need to extract value from the second table row.
<div id="weeklyPrices"> <tr> <td>$56.00</td> <td>$56.00</td> <td>$56.00</td> <td>$56.00</td> <td>$60.00</td> <td>$70.00</td> <td>$70.00</td> </tr> </div>
Looking ahead, the right XPath for that case will be: //div[@id='weeklyPrices']/tr/td1
To use this component, open the JMeter menu and: Add -> Post Processors -> XPath Extractor
XPath Extractor contain several common configuration elements that are mentioned in the ‘Correlation using Regular Expression Extractor’. This includes Name, Apply to, Reference Name, Match No. (since JMeter 3.2) and Default Value.
There are lots of web resources with online cheat sheets and editors to create and test your created xpath (like this one). But based on the examples below, we can find the way to create the most common xpath locators.
If you want to parse HTML into XHTML, we need to check the “Use Tidy” option. After deciding on the “Use Tidy” status, there are also additional options:
If ‘Use Tidy’ is checked:
If ‘Use Tidy’ is unchecked:
‘Return entire XPath fragment instead of text content’ is self descriptive and should be used if you want to return not only the xpath value, but also the value within its xpath locator. It might be useful for debugging needs.
It is also worth mentioning there are list of very convenient browser plugins for testing XPath locators. For Firefox you can use the ‘Firebug’ plugin while for Chrome the ‘XPath Helper’ is the most convenient tool.