这就是HTML的结构;如果兄弟<div>
等于type1,我试图获取<p>
的值
<div class="zsg-lg">
<div class="hdp-fact-ataglance">
<div class="media-bd">
<p>
type1
<div>
value
<div class="zsg-lg">
<div class="hdp-fact-ataglance">
<div class="media-bd">
<p>
type2
<div>
value2
这是我目前无法使用的XPath,我对如何构建它感到很困惑。
div[contains(@class, "zsg-lg")]/div[contains(@class, "hdp-fact-ataglance")]/div[contains(@class, "media-bd") and [p == "Type"]]/div/text()
答案 0 :(得分:1)
我会建议:
normalize-space(
//div[contains(@class, "zsg-lg")]
/div[contains(@class, "hdp-fact-ataglance")]
/div[
contains(@class, "media-bd")
and
normalize-space(p/text())="type1"
]
/div
/text()
)
答案 1 :(得分:0)
看起来语法有点偏,这很有效:
div[contains(@class, "zsg-lg")]/div[contains(@class, "hdp-fact-ataglance")]/div[contains(@class, 'media-bd') and p = 'type1']/div/text()