在用Selenium的python代码去xpath去提取和查找元素了。
现在需要去实现,判断:
a的aria-label不为0 items in shopping cart
就像:
<code><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="{&quot;cN&quot;:&quot;GlobalNav_Cart_nav&quot;,&quot;bhvr&quot;:82,&quot;id&quot;:&quot;n2c10c4c1m1r1a1&quot;,&quot;sN&quot;:2,&quot;aN&quot;:&quot;c10c4c1m1r1a1&quot;}"> <span class="shopping-cart-amount x-hidden-focus" aria-hidden="true">2</span> </a> </code>
表示购物车中有物品。
<code><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="{&quot;cN&quot;:&quot;GlobalNav_Cart_nav&quot;,&quot;bhvr&quot;:82,&quot;id&quot;:&quot;n2c10c1m1r1a1&quot;,&quot;sN&quot;:2,&quot;aN&quot;:&quot;c10c1m1r1a1&quot;}"> <span class="shopping-cart-amount x-hidden" aria-hidden="true">0</span> </a> </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中如何判断属性值不等于某个值