Xpath: how to select nth option

xpath

using xpath, how do you select the n-th option ?

<select>
<option></option>
<option></option>
</select>

/html/body/select/option[?]

Best Answer

what you have is correct:

for the 2nd option, use:

/html/body/select/option[2]

or

/html/body/select/option[position()=2]

See http://www.w3.org/TR/xpath#location-paths

Edit

Note that the above assumes you have a structure like:

 <html>
  <body>
   <select>
    <option></option>
    <option></option>
   </select>
  </body>
 </html>

If your select is inside of a parent other than body, then you either want to use something like:

/html/body/div[@class='header']/select/option[2]

or

//select/option[2]

Of course, since your select probably has a name attribute, you could use that, e.g.

//select[@name='myselect']/option[2]