开发者

xpath: how to select nth option?

开发者 https://www.devze.com 2022-12-10 10:48 出处:网络
using xpath, how do you select the n-th option ? <select> <option></option> <option></option>

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

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

/html/body/select/option[?]开发者_运维知识库


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]
0

精彩评论

暂无评论...
验证码 换一张
取 消