Skip to content

XPath2 Variable Extractor

XPath2 Variable Extractors lets you extract variables from HTML/XML responses using XPath2 queries.

Extracting variables from HTTP response helps you make your Virtual User have a dynamic behavior. Extracted variables can be injected in Logic Actions or HTTP Request Actions.

Warning

The XPath2 extractor consumes more CPU than most regex extractors, try to use it only when relevant. Especially if you need to extract a lot of values this way.

XPath2 Extractor

Field Description
Name Name of the variable created by this processor. Can be used with the ${...} syntax like any other variable.
From This is for information purpose since this processor can only extract from Response body.
Include XML Tags This option will return the entire XPath fragment instead of the text. Use it when you're not simply extracting a single attribute.
Query The XPath Query to be used.
Default value If no value is found, the variable will be equal to this. With match number All, you get the default value if you do not provide an index number.
Match number Lets you choose how to deal with the processor having several results (or occurrences). Selecting All will change the behavior of your processor, see the dedicated section of the regex page for more details.

Namespace

In this section you must specify the XML Namespace of your document:

XPath2 Namespace

Field Description
Prefix Prefix if required. Example: <x xmlns:edi='http://ecommerce.example.org/schema'> here the prefix is edi.
URI Namespace URI. Example: <html xmlns="http://www.w3.org/1999/xhtml"> in this case the URI is http://www.w3.org/1999/xhtml with no Prefix.
add-line Add a new namespace.
remove-line Remove an existing namespace.

Debug

In order to validate the results of your extraction, you can use the debug panel. The procedure is explained on the regex extractor page for both simple and multiple occurrences.

Example

From a browser

In this example we will be using the login page from our Petstore application. Make sure to import an HAR recording of this page.

The first step will be to navigate to this page and use the Right-click + Inspect menu of our browser to locate the element we want to extract:

XPath Extractor check

Using the Copy XPath function we can get:

//*[@id="Catalog"]/form/div/input[1]

We can copy this directly in our XPATH2 extractor in OctoPerf, just adding on top of it that we want to extract the content of the value attribute with /@value:

XPath2 usage

Warning

On top of that you can see that we filled the namespace that was specified in the HTML element.

Simpler expression

Alternatively we could also rework our XPath Query to something simpler like:

//input[@name='_sourcePage']/@value

We would then pick only inputs named _sourcePage and this would be less dependent on the page structure. So provided the name is unique to this parameter we can get a more resilient expression if the application ever changes.

Tip

You can use third party tools like XPather to accelerate the design of your XPath expression. Just copy and paste the content of the response displayed on this page.