xpath for selecting <option> html tag?
Asked Answered
W

2

13

xpath for selecting html tag ?

<select>
<option value="first option"> 1 </option>
<option value="second option"> 2 </option>
<option value="third option"> 3 </option>
</select>

Would below suffice ?

html/body/form/select[@name='options' and @value='first option']
Weywadt answered 8/10, 2009 at 2:57 Comment(0)
D
17

Several options here:

  • /html/body/form/select/option
  • /html/body/form/select/option[1]
  • /html/body/form/select/option[position() = 1]
  • /html/body/form/select/option[@value='first option']

All these lead to first option element

Depilate answered 8/10, 2009 at 3:2 Comment(5)
what about for select with multiple options ? how can you select more than one option ?Weywadt
The first option by Rubens (shortened: //select/option) selects in fact all option tags. Also from other select tags, if you have more than one.Hachure
its right, andre-r; returning one or several elements depends on SelectSingleNode/SelectNodes method usageDepilate
can one select the option by what is inside the tag? eg, not with the index, [1], but something like (unsuccessfully) //select/option[@label='1']Gault
you can do //select/option[. = '1']Depilate
M
8

Another option:

//select[@id='id']/option[text() = 'option text']
Matildamatilde answered 17/10, 2014 at 20:25 Comment(1)
Is it possible to use trimmed values ?Nims

© 2022 - 2024 — McMap. All rights reserved.