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

automated-tests, jmeter, load-testing, xpath

Solution

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.

Problem

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?

Original source