如何使用xPath选择特定标签的属性值?

时间:2019-07-01 15:05:52

标签: html xml xpath

我需要从中获取文本, <option value="......" >.......,但第一个选项标签除外。

我的代码是:

"xPath":"descendant::div[contains(@class,'produit__select')]/descendant::select[@name='select0']/descendant::option[@disabled='disabled']/value"

我无法获得结果。有人可以帮我这个忙吗?

<div class="produit__select">
 <label for="select0">Parfum</label>
 <select name="select0" id="select0">
   <option disabled="disabled">Faites un choix</option>  
   <option value="835">Pommes de Nouvelle Aquitaine</option> 
   <option value="842">Pommes Nectarines Bananes</option>
   <option value="838">Pommes Pruneaux</option>
   <option value="839" selected="selected">Pommes Fraises</option>
   <option value="841">Pommes Cassis</option>
   <option value="840">Pommes Bananes Kiwi</option>
   <option value="836">Pommes Poires</option>
   <option value="837">Pommes Coings</option> 
 </select>
</div>

2 个答案:

答案 0 :(得分:2)

您可以使用//代替descendant::。另外,没有名为value的元素。如果您对文本感兴趣,请使用text()函数,如果要跳过第一个孩子,请确保该孩子的position()大于1。

//div[contains(@class,'produit__select')]
/select[@name='select0']
/option[position()>1]/text()

答案 1 :(得分:1)

这是跳过禁用的另一个选项。

//div[@class='produit__select']/select[@name='select0']/option[not(contains(@disabled,'disabled'))]/text()

演示:

enter image description here