如何选择与同一级别的其他元素相关的元素

如何选择与同一级别的其他元素相关的元素

本文介绍了XPath:如何选择与同一级别的其他元素相关的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单,但我没有足够的练习来解决这个问题:)

如果我们知道我们只需要 item_promo 元素,如何从块"内的每个 div 中获取 price 文本值.

<div class="item_promo">item</div><div class="item_price">123</div>

<div class="block"><div class="item_promo">item</div><div class="item_price">456</div>

<div class="block"><div class="item_promo">item</div><div class="item_price">789</div>

<div class="block"><div class="item">item</div><div class="item_price">222</div>

<div class="block"><div class="item">item</div><div class="item_price">333</div>

解决方案

你可以使用 xpath :

//div[@class='block']/*[@class='item_promo']/following-sibling::div[@class='item_price']/text()

您查找具有属性 class 且值为 item_promodiv 元素,并查看其以下具有属性 item_price 的兄弟元素 并抓取文本.

The question is simple but I don't have enough practice for this case :)

How to get price text value from every div within "block" if we know that we need only item_promo elements.

<div class="block">
     <div class="item_promo">item</div>
     <div class="item_price">123</div>
</div>
<div class="block">
     <div class="item_promo">item</div>
     <div class="item_price">456</div>
</div>
<div class="block">
     <div class="item_promo">item</div>
     <div class="item_price">789</div>
</div>
<div class="block">
     <div class="item">item</div>
     <div class="item_price">222</div>
</div>
<div class="block">
     <div class="item">item</div>
     <div class="item_price">333</div>
</div>
解决方案

You could use the xpath :

//div[@class='block']/*[@class='item_promo']/following-sibling::div[@class='item_price']/text()

You look for div elements that has attribute class with value item_promo and look at its following sibling which has an attribute item_price and grab the text.

这篇关于XPath:如何选择与同一级别的其他元素相关的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:02