Xpath:如何根据文本而非值属性选择选项? [英] Xpath: how to select an option based on its text not value property?

查看:35
本文介绍了Xpath:如何根据文本而非值属性选择选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑两种类型:

<select name="garden">
    <option>Flowers</option>
    <option selected="selected">Shrubs</option>
    <option>Trees</option>
    <option selected="selected">Bushes</option>
    <option>Grass</option>
    <option>Dirt</option>
</select>

@val 是否用于实际指示 value="" 属性?

Is @val for actually indicating the value="" attribute ?

@value 是用来表示innerText 值的吗?

Is @value for indicating the innerText value ?

例如,如果 不包含任何 value="" 属性会发生什么.那你会怎么选?

for example what happens if <option> doesn't contain any value="" property. how would you select it then ?

select/option[@value = "Grass"] 

对于上述情况,Xpath 是否会自动忽略空格?应该修剪吗?

Does Xpath automatically ignore white spaces for the case above? Should it be trimmed?

选择多个选项就够了吗?

for selecting multiple options would this suffice ?

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

推荐答案

要通过文本值进行选择,您可以使用 text() 函数.并且规范化空格是必需的,因为默认情况下它们不会被删除.下面是一个例子:

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

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

@value - value"属性的值

@value - value of "value" attribute

@val - val"属性的值

@val - value of "val" attribute

normalize-space()- 函数返回带有空格的参数字符串,通过剥离前导和尾随空格并用单个空格替换空格字符序列进行标准化

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

这篇关于Xpath:如何根据文本而非值属性选择选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆