2
votes

I get a xml and want to extract the value of one element by XPath Extractor, however it returns "Assertion failure message: -1" in jmeter.

<flag>
    <sample>123</sample>
    <others>...</others>
    <target>a test sample</target>
</flag>
<flag>
    <sample>1234</sample>
    <others>...</others>
    <target>a test sample</target>
</flag>

In order to get the text of element <target> where the <sample> is 123, several ways have been tried but failed:

  • //flag[sample='123']/target
  • //sample[.='123']/../target
  • //sample[@value='123']/../target/@value
  • //sample[text()='123']/../target/text()

Is there a way to capture the value of element <target>?

3
1st one is the correct XPath in your scenario... - Amrendra Kumar
Probably a typo in your example but <others>...<others> should be <others>...</others> - Nesku

3 Answers

0
votes

You need to be sure of the action you want to do, it is a big difference between getting the text and getting the element.

For finding the element you have at least 3 options:

  1. find the target from flag that has a sample child with 123 text
    //flag[./sample[textt()='123']]/target

  2. find the sample that has 123 text, go to parent and find target
    //sample[textt()='123']/../target

  3. find the sample with that has text 123 and get the target sibling
    this you need to discover yourself

Now you have the object element that you can use. If you want the text then you need to add /text()

0
votes

You should use something like:

//sample[text()='123']/parent::*/target/text()='a test sample'

Demo (assumes XPath Tester mode of the View Results Tree listener):

enter image description here

References:

0
votes

Thank you all, I don't know why all the solution failed; instead, a Regular Expression Extractor works. Maybe XPath Extractor just can't handle those kind of cases.