Xpath: how to choose the nth option?

using xpath, how do you choose the nth option?

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

/ html / body / select the option / [?]

+3
source share
1 answer

what you have right:

for the second 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 that you have a structure such as:

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

If your choice is inside a parent other than the 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 choice probably has a name attribute, you can use it, for example.

//select[@name='myselect']/option[2]
+8
source

Source: https://habr.com/ru/post/1721663/


All Articles