首页 > 解决方案 > 无法使用 position()、last()、[1] indexing([1]--eg.) 找到 xpath 如下:兄弟姐妹、孩子、父母 没有任何工作

问题描述

我正在尝试找出 XPATH。我得到一个元素的 30 个匹配项。

我尝试使用 last()、position()、indexing[]、child、parent、following:siblings、previous:siblings,absolute XPATH 也不起作用,如何实现这一点。

<div data-reactid=".0.0.1.0.0.1.$termsConditions-component.0.1.3.1.0.0.0.0.0.2.0:$5.5.0"><span class="cursor" data-reactid=".0.0.1.0.0.1.$termsConditions-component.0.1.3.1.0.0.0.0.0.2.0:$5.5.0.0"><svg viewBox="0 0 16 20" class="excel-document" style="background-image:none;background-position:0% 0%;display:inline;fill:#333;height:20px;width:16px;" data-reactid=".0.0.1.0.0.1.$termsConditions-component.0.1.3.1.0.0.0.0.0.2.0:$5.5.0.0.0"><path d="M2 0C.9 0 .01.9.01 2L0 18c0 1.1.89 2 1.99 2H14c1.1 0 2-.9 2-2V6l-6-6H2zm7 7V1.5L14.5 7H9zm-2.34 7.39l-3.45-5.1h2.73l2.1 3.42 2.175-3.42h2.58L9.36 14.405 13.095 20H10.29l-2.34-3.705L5.565 20h-2.64l3.735-5.61z" fill="#11764B" data-reactid=".0.0.1.0.0.1.$termsConditions-component.0.1.3.1.0.0.0.0.0.2.0:$5.5.0.0.0.0"></path></svg></span>

标签: selenium-webdriverxpath

解决方案


如果您尝试访问给定 xpath 的第一个匹配元素,则将整个 xpath 包装起来(),然后提供如下所示的索引。

第一个匹配元素:

 (//span[@class='cursor']//*[local-name()='svg'][@class='excel-document'])[1]

跟随元素到第一个匹配的元素:

(//span[@class='cursor']//*[local-name()='svg'][@class='excel-document'])[1]/following-sibling::*

最后一个元素:

 (//span[@class='cursor']//*[local-name()='svg'][@class='excel-document'])[last()]

在此处输入图像描述


推荐阅读