我的html看起来像这样:
div class = 'a-row a-spacing-small'
div class = 'a-row'
(这些位于同一级别,我的意思是第二div不是第一个div的孩子(他们两个都是同一级别的父母)
我想从第一个div
的内部级别中选择href
,其中class = 'a-row a-spacing-small'
仅在第二个DIV的内部级别中的某些内容满足条件时才CC_3。
我该怎么做?
任何想法
因此,对于xpath,您想从可以轻松识别的元素开始,或者在这种情况下,在这种情况下,是第一个div
,其中类是'a-row a-spacing-small'
:
//div[@class='a-row a-spacing-small']
接下来是确定您的元素与此根元素的关系,因此,在这种情况下,您想要一个href
,它将在a
中,并且应该在我们确定的div
中。但是您不确定是否确定是否确定这是一个直接的孩子..所以您使用//
:
//div[@class='a-row a-spacing-small']//a/@href
但是,如果旁边的div
满足条件。
//<somexpath>/div[@class='a-row a-spacing-small']//a/@href
但是什么XPath?我们知道的是我们将第二个 div
成为根,因为它具有我们想要的条件...
//div[@class='a-row']
那么,第一个div
与我们的新根有什么关系?它是preceding-sibling
,因为它是在新的根部之前,并且在同一级别上。
//div[@class='a-row']/preceding-sibling::div[@class='a-row a-spacing-small']//a/@href
现在无论情况如何,我们都需要将其包括在根...
//div[@class='a-row' and <condition>]/preceding-sibling::div[@class='a-row a-spacing-small']//a/@href
示例:
如果条件是DIV应具有带有文本"条件链接"的a
: //div[@class='a-row' and .//a[text()='conditional link']]/preceding-sibling::div[@class='a-row a-spacing-small']//a/@href
如果条件是DIV应具有禁用属性: //div[@class='a-row' and @disabled]/preceding-sibling::div[@class='a-row a-spacing-small']//a/@href
无禁用属性怎么样? //div[@class='a-row' and not(@disabled)]/preceding-sibling::div[@class='a-row a-spacing-small']//a/@href
玩它...