September 23, 2020

Using the XPath Extractor in JMeter

Open Source Automation

The XPath Extractor is a useful component of JMeter. But how should you use it — and when? Find out in this blog.

Back to top

What Is the XPath Extractor in JMeter?

XPath Extractor is a post-processor component of JMeter that makes it easy to extract data when writing JMeter Scripts. 

This is useful if it is overly difficult to extract information using the Regular Expression Extractor. (For example, you may have encountered scenarios where there are some similar tags with no attributes – but different values.)

 

A screenshot of tags with unique values.

 

If you use the Regular Expression Extractor, your RegEx pattern will match all three values, as there are no attributes in the tag div. But with the XPath Extractor, it’s easy.

 

Back to top

 XPath Extractor vs. Alternatives

XPath vs. JSON Path

JSON Path uses queries that are similar to XPath to match values. However, the syntax is a little bit different.

A screenshot breaking down the differences between XPath and JSONPath.

Related Resources:

XPath Extractor vs. RegExp Extractor

The following compares the XPath and Regexp Extractors.

The Pros 

  • Easy to parse HTML tags without attributes.
  • Easy to parse RSS feeds.
  • The ability to use Tidy (tolerant parser).
  • You can parse JSON with same attribute names.

The Cons 

  • Builds DOM tree to parse HTML code, it consumes CPU and memory

Which Is Better?

When you have a choice of the XPath or Regexp Extractor, we recommend the latter because it works faster. If possible, rewrite your XPath queries to Regexp queries when you want to tune your script and the JMeter server’s capabilities are very limited. But that doesn’t mean that you shouldn’t use XPath Extractor. If you have some XPath samplers in the test tree, it’s fine.

Back to top

How to Use the XPath Extractor

Add the Extractor

To use the XPath Extractor, add it as a child element to the HTTP Request sampler (or any other sampler, for that matter). (As the target server we'll be using our favorite site, blazemeter.com.)

Xpath Extractor as a child element to HTTP Request sample.

 

 

Add the Extractor as a child element of the HTTP Request:

 

Add XPath Extractor as child element of HTTP Request:

 

After running the test, the response body of the parent element will be sent to the XPath Extractor to parse. Now we can extract value(s) from the structured response - XML or (X)HTML, using XPath query language. Let’s create a simple XPath query that extracts the title of the page.

 

Xpath query which will extract title of the page

 

Run your script as though there are no errors in the log. This means you did everything correctly. Note: You won’t see any parsed results, which is fine. We'll come across them in the debugging block.

 

The main elements which where used to parse Xpath Extractor.

Definitions of 'elementus majorus'

 

Debug Your XPath Query

To debug our XPath query and see the results, let’s add two samplers:

Add -> Sampler -> Debug Sampler

Add -> Listener -> View Results Tree

Now that we see the following results, we are ready to debug the XPath query.

 View Results Tree is ready for us to debug our Xpath query.

 

Let’s re-run our test script and open the “View Results Tree.” In the “Response data” tab, you can see results of query. 

Open “View Results Tree” to see the results.

 

Parsed information can be used as a variable in the next samplers as ${Title} or ${Title_1}. It doesn’t matter, since the query matched only one value. So what if more values are matched?

Let’s change the XPath query to /html/head/meta/@name and re-run the test.

Here, the debug info will be little bit different:

  • Title=generator
  • Title_1=generator
  • Title_2=generator
  • Title_3=description
  • Title_4=OBKey
  • Title_5=keywords
  • Title_6=MobileOptimized
  • Title_7=HandheldFriendly
  • Title_8=viewport
  • Title_matchNr=8

 

Title_matchNr=8 means that we have eight matches and can use any of them as ${Title_numberFrom1To8}.

Recall that, in our first example, we couldn’t parse with the RegEx extractor. In XPath, we can match any value of a tag by specifying the number of divs. Here, the query: .//*[@id='numbers']/div[2] - matches the second div and .//*

[@id='numbers']/div[3] matches the third one. IMPORTANT: When you copy and paste XPath queries, be sure that no curly apostrophes are copied.

Parse JSON

Use the JMeter Plugins Manager and add JSON plugins.

To ensure that everything is installed correctly, add the OAuth Sampler in a JMeter test plan (JMeter menu -> Edit -> Add -> Sampler -> OAuth Sampler). Now try to parse some JSON data. To do this, we don’t need to search for any public JSON API. We can just use BlazeMeter's API.

  1. Add the HTTP Request sampler.
  2. Insert this URL-> https://a.blazemeter.com/api/rest/blazemeter/testGetStatus/
  3. Add two JSON Path Extractors as the child/ren.
  4. Insert any name in Name field and response_code in JSON path field.
  5. Insert any name in Name field and error in JSON path field.
  6. Add Debug sampler and View Results Tree listener.
  7. Run it.
Insert any name in “Name” field and response_code in “JSON path” field

 

In the “View Results Tree” listener, you will see that the JSON data was parsed successfully.

In “View Results Tree” listener you will see that JSON data was parsed successfully.

 

Write XPath Queries

Check out some additional XPath documentation to master some of the basics. But you needn’t remember everything. This blog post can be a reference and you can always ease your work with some tools such as Firebug and FirePath add-ons.

After opening the page from which you want to extract the XPath, and then launch Firebug. Select FirePath and click the Analyze button. Move the mouse to any element of the page and you will see XPath queries in the FirePath field.

 

How to write XPath queries

 

We got the query .//*[@id='edit-title'] , which will match the selected elements. Using this add-on, you can analyze any element of the page without needing to check the XPath reference each time to remember syntax.

More examples of HTML code.
Another screenshot of HTML code.

 

A third screenshot of HTML code.

 

A screenshot of an XPath Query.
 
This blog was originally published on September 23, 2014 and has since been updated for accuracy and relevance.
 

START TESTING NOW

 

Related Resources:

Back to top