4

I want to extract the first value which has the property selected = "selected" using XPath extractor. But it doesn't seem to work for me.

The html which i'm extracting the value from is:

< select id="ddLocation" name="ddLocation" class="DDlocation" size="1"      onchange="jsf.util.chain(this,event,'onLocationChange();,'mojarra.ab(this,event,\'valueChange\',\'@this\',0)')"> <br>
    < option value="43" selected="selected">Pune</option> <br>
    < option value="44">Agra< /option> <br>
    < option value="45">Guntur< /option> <br>
    < option value="46">Kochi< /option> <br>
    < option value="73">Kothrud< /option> <br>
    < option value="153">Ratnagiri< /option> <br>
    < option value="156">Baner< /option>

My XPath query is:

//select[@id="ddLocation"]/option[1]/@value

Is it wrong?
Can anyone suggest me any better / right approach please?

2 Answers 2

7

Your xml is not in proper format

It has lot of spaces in-front of option and select is not closed at end.

<select id="ddLocation" name="ddLocation" class="DDlocation" size="1" onchange="jsf.util.chain(this,event,'onLocationChange();,'mojarra.ab(this,event,\'valueChange\',\'@this\',0)')">
    <option value="43" selected="selected">Pune </option>
    <option value="44">Agra</option>
    <option value="45">Guntur</option>
    <option value="46">Kochi</option>
    <option value="73">Kothrud</option>
    <option value="153">Ratnagiri</option>
    <option value="156">Baner</option>
</select>

Finally, your XPATH works as expected.

//select[@id="ddLocation"]/option[1]/@value

It gives output as 43

EDIT:

If you use below XPATH, it gives result according to where attribute is selected=selected

//select[@id='ddLocation']/option[@selected='selected']/@value

I have not tested using JMeter but am checking XPATH on XMLSPY.

2
  • Sorry, I purposely kept spaces in-front of option and select because I was not aware of any other way to display raw html source in my query here on stackoverflow. It was getting converted to actual select options in the preview hence I put those spaces. The actual html source is exactly what you pasted. I don't know why the XPath query is not working in JMeter. Did you get the output as 43 in JMeter or you used a XPath evaluator to do that? I've added the extractor to the sampler which gives me the above response. And i've used the extractor's reference name as a parameter in another request. Jul 10, 2012 at 7:39
  • @AbhijeetVaikar: I have not used JMeter but am using XMLSPY to test the XPATH. I have updated answer with another XPATH which will give result according to where selected attribute is available Jul 10, 2012 at 8:47
1

Since you are using XPath Extractor to parse HTML (not XML!..) response ensure that Use Tidy (tolerant parser) option is CHECKED (in XPath Extractor's control panel).

And use better refined xpath query from Siva's answer below.

1
  • Yeah I did not have Use Tidy (tolerant parser) checked. Thank you! And yes I used the refined xpath query from Siva's answer. Jul 10, 2012 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.