possible to select multiple options with xpath?
/html/body/form/select/option[@val = '1' and @val = '3']
so that means select the first a开发者_开发知识库nd third option in a select-multiple form ?
Did you mean or
?
/html/body/form/select/option[@val = '1' or @val = '3']
That should select both elements. By using and
you're trying to select an element whose val
is both 1
and 3
, which isn't going to work. :-)
To be clear, by select you mean it returns those nodes in the results, right? Not actually selects it in the DOM?
Either way, what you want is the following:
/html/body/form/select/option[1|3]
or
/html/body/form/select/option[position()=1|postion()=3]
Notice the use of the |
meaning "or", you don't want to use "and" because that means you want all results that are both in position 1 and 3, which is impossible. You want all results that are in either position 1 or 3.
Almost. A predicate (everything inside the square brackets) specifies the condition for including a specific node in the result set, and it's impossible for any one element to have a val
value of 1 and 3 at the same time. If you replace the and
with or
, however, the expression will match any node that meets either criteria:
/html/body/form/select/option[@val = '1' or @val = '3']
A side note: If you want to select nodes based on their position and not their attribute values, you can use the position()
function inside the predicate.
精彩评论