Skip to content

XPath Extractor

Configure the JMeter XPath Extractor post-processors: properties, defaults, and practical usage notes for building reliable load tests.

Difficulty
intermediate
Guide type
reference
Estimated read time
4 min read
Last verified version
Verified JMeter 5.6

Part of the Post-Processors category. Also documented in context in the full Component Reference.

XPath Extractor

This test element allows the user to extract value(s) from structured response - XML or (X)HTML - using XPath query language.

NameRequiredDescription
NameNoDescriptive name for this element that is shown in the tree.
Apply to:YesThis is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller. - Main sample only - only applies to the main sample - Sub-samples only - only applies to the sub-samples - Main sample and sub-samples - applies to both. - JMeter Variable Name to use - extraction is to be applied to the contents of the named variable XPath matching is applied to all qualifying samples in turn, and all the matching results will be returned.
Use Tidy (tolerant parser)YesIf checked use Tidy to parse HTML response into XHTML. - โ€œUse Tidyโ€ should be checked on for HTML response. Such response is converted to valid XHTML (XML compatible HTML) using Tidy - โ€œUse Tidyโ€ should be unchecked for both XHTML or XML response (for example RSS) :::note For HTML, CSS Selector Extractor is the correct and performing solution. Donโ€™t use XPath for HTML extractions. :::
QuietIf Tidy is selectedSets the Tidy Quiet flag
Report ErrorsIf Tidy is selectedIf a Tidy error occurs, then set the Assertion accordingly
Show warningsIf Tidy is selectedSets the Tidy showWarnings option
Use NamespacesIf Tidy is not selectedIf checked, then the XML parser will use namespace resolution.(see note below on NAMESPACES) Note that currently only namespaces declared on the root element will be recognised. See below for user-definition of additional workspace names.
Validate XMLIf Tidy is not selectedCheck the document against its schema.
Ignore WhitespaceIf Tidy is not selectedIgnore Element Whitespace.
Fetch External DTDsIf Tidy is not selectedIf selected, external DTDs are fetched.
Return entire XPath fragment instead of text contentYesIf selected, the fragment will be returned rather than the text content. For example //title would return โ€œ<title>Apache JMeter</title>โ€ rather than โ€œApache JMeterโ€. In this case, //title/text() would return โ€œApache JMeterโ€.
Name of created variableYesThe name of the JMeter variable in which to store the result.
XPath QueryYesElement query in XPath language. Can return more than one match.
Match No. (0 for Random)NoIf the XPath Path query leads to many results, you can choose which one(s) to extract as Variables: - 0: means random - -1 means extract all results (default value), they will be named as _<variable name>__N (where N goes from 1 to Number of results) - X: means extract the Xth result. If this Xth is greater than number of matches, then nothing is returned. Default value will be used
Default ValueNoDefault value returned when no match found. It is also returned if the node has no value and the fragment option is not selected.

To allow for use in a ForEach Controller, the following variables are set on return:

  • refName - set to first (or only) match; if no match, then set to default
  • refName_matchNr - set to number of matches (may be 0)
  • refName_n - n=1, 2, 3, etc. Set to the 1st, 2nd 3rd match etc.

XPath is query language targeted primarily for XSLT transformations. However it is useful as generic query language for structured data too. See XPath Reference or XPath specification for more information. Here are few examples:

/html/head/title : extracts title element from HTML response

/book/page[2] : extracts 2nd page from a book

/book/page : extracts all pages from a book

//form[@name='countryForm']//select[@name='country']/option[text()='Czech Republic'])/@value : extracts value attribute of option element that match text โ€˜Czech Republicโ€™ inside of select element with name attribute โ€˜countryโ€™ inside of form with name attribute โ€˜countryFormโ€™

Another option is to use the following code:

//mynamespace:tagname

by:

//*[local-name()='tagname' and namespace-uri()='uri-for-namespace']

where โ€œuri-for-namespaceโ€ is the uri for the โ€œmynamespaceโ€ namespace.(not applicable if Tidy is selected)

On this page