BreadcrumbHomeResourcesBlog Using The XPath Extractor In JMeter September 23, 2020 Using the XPath Extractor in JMeterOpen Source AutomationBy Bob MelievThe XPath Extractor is a useful component of JMeter. But how should you use it — and when? Find out in this blog.Table of ContentsWhat Is the XPath Extractor in JMeter? XPath Extractor vs. AlternativesHow to Use the XPath ExtractorTable of Contents1 - What Is the XPath Extractor in JMeter?2 - XPath Extractor vs. Alternatives3 - How to Use the XPath ExtractorBack to topWhat 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.) 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. AlternativesXPath vs. JSON PathJSON Path uses queries that are similar to XPath to match values. However, the syntax is a little bit different.Related Resources:JSONPath syntaxEasily debugging your JSONPath queriesXPath Extractor vs. RegExp ExtractorThe 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 memoryWhich 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 topHow to Use the XPath ExtractorAdd the ExtractorTo 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.) Add the Extractor as a child element of the 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. 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. Definitions of 'elementus majorus' Debug Your XPath QueryTo debug our XPath query and see the results, let’s add two samplers:Add -> Sampler -> Debug SamplerAdd -> Listener -> View Results TreeNow that we see the following results, we are ready to debug the 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. 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=generatorTitle_1=generatorTitle_2=generatorTitle_3=descriptionTitle_4=OBKeyTitle_5=keywordsTitle_6=MobileOptimizedTitle_7=HandheldFriendlyTitle_8=viewportTitle_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 JSONUse 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.Add the HTTP Request sampler.Insert this URL-> https://a.blazemeter.com/api/rest/blazemeter/testGetStatus/Add two JSON Path Extractors as the child/ren.Insert any name in Name field and response_code in JSON path field.Insert any name in Name field and error in JSON path field.Add Debug sampler and View Results Tree listener.Run it. In the “View Results Tree” listener, you will see that the JSON data was parsed successfully. Write XPath QueriesCheck 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. 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. This blog was originally published on September 23, 2014 and has since been updated for accuracy and relevance. START TESTING NOW Related Resources:JMeter's JSON Path Extractor Plugin - Advanced Usage ScenariosThe Boundary Extractor vs. the Regular Expression Extractor in JMeterBack to top