最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Xpath中如何判断属性值不等于某个值

XPath crifan 7641浏览 0评论

在用Selenium的python代码去xpath去提取和查找元素了。

现在需要去实现,判断:

a的aria-label不为0 items in shopping cart

就像:

<code>&lt;a id="uhf-shopping-cart" aria-label="2 items in shopping cart" class="c-action-trigger c-glyph glyph-shopping-cart" href="https://www.microsoft.com/en-us/store/buy" data-m="{&amp;quot;cN&amp;quot;:&amp;quot;GlobalNav_Cart_nav&amp;quot;,&amp;quot;bhvr&amp;quot;:82,&amp;quot;id&amp;quot;:&amp;quot;n2c10c4c1m1r1a1&amp;quot;,&amp;quot;sN&amp;quot;:2,&amp;quot;aN&amp;quot;:&amp;quot;c10c4c1m1r1a1&amp;quot;}"&gt;
            &lt;span class="shopping-cart-amount x-hidden-focus" aria-hidden="true"&gt;2&lt;/span&gt;
        &lt;/a&gt;

</code>

表示购物车中有物品。

<code>&lt;a id="uhf-shopping-cart" aria-label="0 items in shopping cart" class="c-action-trigger c-glyph glyph-shopping-cart x-hidden-focus" href="https://www.microsoft.com/en-us/store/buy" data-m="{&amp;quot;cN&amp;quot;:&amp;quot;GlobalNav_Cart_nav&amp;quot;,&amp;quot;bhvr&amp;quot;:82,&amp;quot;id&amp;quot;:&amp;quot;n2c10c1m1r1a1&amp;quot;,&amp;quot;sN&amp;quot;:2,&amp;quot;aN&amp;quot;:&amp;quot;c10c1m1r1a1&amp;quot;}"&gt;
            &lt;span class="shopping-cart-amount x-hidden" aria-hidden="true"&gt;0&lt;/span&gt;
        &lt;/a&gt;
</code>

表示没有。

想要写成:

<code>notEmptyCartElem = driver.find_element_by_xpath('//a[@id="uhf-shopping-cart" and @aria-label!="0 items in shopping cart"]')
</code>

结果发现:

xpath property no equal

Xpath test for ancestor attribute not equal string – Stack Overflow

xml – XPath for elements with attribute not equal or does not exist – Stack Overflow

equals – XPath operator “!=”. How does it work? – Stack Overflow

有推荐用:

not(@someProperty=“some text value”)

的。

去试试,结果是:

<code>notEmptyCartElem = driver.find_element_by_xpath('//a[@id="uhf-shopping-cart" and @aria-label!="0 items in shopping cart"]')
</code>

是可以正常工作的。

【总结】

此处的Selenium的Chrome的driver中用xpath去查找元素,是可以通过:

<code>//a[@id="uhf-shopping-cart" and @aria-label!="0 items in shopping cart"]
</code>

实现判断,只匹配到

<code>id="uhf-shopping-cart" aria-label="2 items in shopping cart" 
</code>

这类元素,而不匹配:

<code>id="uhf-shopping-cart" aria-label=“0 items in shopping cart"
</code>

即xpath支持:

<code>@someProperty!=“some not expected text”
</code>

这种写法的

转载请注明:在路上 » 【已解决】Xpath中如何判断属性值不等于某个值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.176 seconds, using 21.99MB memory