[ACCEPTED]-Xpath: how to select an option based on its text not value property?-xpath

Accepted answer
Score: 35

To select by text value you can use text() function. And 8 normalize spaces is required, because they 7 are not removed by default. Here is an example:

select/option[normalize-space(text())="Grass"]

@value - value 6 of "value" attribute

@val - value of 5 "val" attribute

normalize-space() - function returns 4 the argument string with whitespace normalized 3 by stripping leading and trailing whitespace 2 and replacing sequences of whitespace characters 1 by a single space

Score: 12

Well, if whitespace isn't an issue:

/select/option[.='Grass']

I'd need 2 to check re whitespace, though. You could 1 always normalize:

/select/option[normalize-space(.)='Grass']

More Related questions