Abhijeet Vaikar
Abhijeet Vaikar

Reputation: 1656

Extracting value from select element in HTML using XPath Query in JMeter

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?

Upvotes: 4

Views: 9771

Answers (2)

Aliaksandr Belik
Aliaksandr Belik

Reputation: 12873

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.

Upvotes: 1

Siva Charan
Siva Charan

Reputation: 18064

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.

Upvotes: 7

Related Questions