Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 884
Abhijeet Vaikar Avatar asked Feb 19 '23 22:02

Abhijeet Vaikar


2 Answers

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.

like image 194
Siva Charan Avatar answered Apr 27 '23 03:04

Siva Charan


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.

like image 35
Aliaksandr Belik Avatar answered Apr 27 '23 03:04

Aliaksandr Belik