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

标签 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>

网页中有上述元素,我想获取带有文本“结束(成功)”标签的流结束节点。我已经尝试了以下两种方法,想了解为什么第一种方法不起作用

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

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

最佳答案

您的第一个 XPath 选择两个节点,因为文档中有两个 flow-end-nodes。您的谓词仅检查文档中任何位置是否存在标签,而不是flow-end-node<的后代.

要更正,请从绝对 //label 更改,

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

相对于.//标签,

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

然后,正如预期的那样,只会选择一个flow-end-node

关于html - 为什么我的 XPath 忽略谓词并选择多个节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51228923/

相关文章:

javascript - 如何改变输入值?

javascript - 使用 Selenium RC 捕获浏览器

javascript - 如何在浏览器调整大小时调整 div 大小

javascript - 嵌入式 Youtube 视频需要点击两次才能在移动设备上播放

html - 博主:如何完全禁用Lightbox?

xml - 是否可以在 schematron 规则中获取完整的 xpath?

html - css - 如果浏览器对齐 div 以占据全高

java - DialogFragment 中的 getContentResolver() 和 getWindow()

xml - Arxml中有命名规则吗?

java - 测试失败时不会截取 Allure 报告截图