为什么我的XPath忽略谓词并选择多个节点?

时间:2018-07-08 04:50:58

标签: html xml selenium xpath

<flow-end-node _nghost-c50="" id="T000k2d27hnb" class="jtk-draggable jtk-droppable ng-star-inserted jtk-endpoint-anchor jtk-connected xh-highlight" style="top: 160px; left: 592px; z-index: 102;">
<div _ngcontent-c50="" class="invisible-node-holder node-setup">
  <div _ngcontent-c50="" class="node-anchor left target"></div>
  <button _ngcontent-c50="" class="mat-fab" mat-fab="" style="background: rgb(66, 189, 65);"><span class="mat-button-wrapper">
    <mat-icon _ngcontent-c50="" class="mat-icon material-icons" role="img" aria-hidden="true">done</mat-icon>
    <flow-notification-icon _ngcontent-c50="" _nghost-c48="">

<!---->
</flow-notification-icon>
  </span><div class="mat-button-ripple mat-ripple mat-button-ripple-round" matripple=""></div><div class="mat-button-focus-overlay"></div></button>
  <label _ngcontent-c50="">End (success)</label>
</div></flow-end-node>

<flow-end-node _nghost-c50="" id="T000k2d27hnc" class="jtk-draggable jtk-droppable ng-star-inserted jtk-endpoint-anchor jtk-connected xh-highlight" style="top: 368px; left: 816px; z-index: 103;">

<div _ngcontent-c50="" class="invisible-node-holder node-setup">
  <div _ngcontent-c50="" class="node-anchor left target"></div>
  <button _ngcontent-c50="" class="mat-fab" mat-fab="" style="background: rgb(230, 42, 16);"><span class="mat-button-wrapper">
    <mat-icon _ngcontent-c50="" class="mat-icon material-icons" role="img" aria-hidden="true">clear</mat-icon>
    <flow-notification-icon _ngcontent-c50="" _nghost-c48="">

<!---->

</flow-notification-icon>
  </span><div class="mat-button-ripple mat-ripple mat-button-ripple-round" matripple=""></div><div class="mat-button-focus-overlay"></div></button>
  <label _ngcontent-c50="">End (failure)</label>
</div></flow-end-node>

在网页中具有上述元素,我想获取带有标签为“ End(成功)”的标签的flow-end-node。我尝试了以下两种方法,并且想了解为什么第一种方法不起作用

  1. //flow-end-node[//label[text()='End (success)']](这个给了我两个节点)

  2. //label[text()='End (success)']/ancestor::flow-end-node(工作正常)

2 个答案:

答案 0 :(得分:1)

您的第一个XPath选择了两个节点,因为您的文档中有两个flow-end-nodes。您的谓词仅检查label 文档中任何地方的存在 ,而不是flow-end-node的后代。

要进行更正,请从绝对值//label更改为

//flow-end-node[//label[text()='End (success)']]

相对于亲戚.//label

//flow-end-node[.//label[text()='End (success)']]

,然后将仅选择一个flow-end-node

答案 1 :(得分:1)

//flow-end-node[descendant::label[. = 'End (success)']]