什么是通过Selenium-webdriver找到相对难以区分标签下的文本内容的最佳方法?

huangapple go评论49阅读模式
英文:

What is the best way to find text content under relatively indistinguishable tags by Selenium-webdriver?

问题

作为新手,我更喜欢使用绝对 XPath 来查找文本定位的 WebElements。我尝试过:

List<WebElement> elements = web.findElements(By.xpath("/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div/div[1]/ol[5]/li[1]/div[2]/div/p"));

但是我无法捕获标签下带有轻微更改的文本。目标 XPath 如下:

/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[6]/div[1]/ol[5]/li[1]/div[2]/div[4]/div[1]/div[1]/p[1]
/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[6]/div[1]/ol[5]/li[1]/div[2]/div[8]/p
/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[2]/div[1]/ol[5]/li[1]/div[2]/div[3]/h3[1]
/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[2]/div[1]/ol[5]/li[1]/div[2]/div[2]/div[1]/p
/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[5]/div[1]/ol[5]/li[1]/div[2]/div[1]/div[1]/p[1]/strong[1]

在上述提到的 XPath 中获取所有文本内容的正确公式或方法是什么?

英文:

As newbie I prefer use Abs XPath to get find WebElemnts where text is positioned.<br>
I tried:

List&lt;WebElement&gt; elements = web.findElements(By.xpath(&quot;/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div/div[1]/ol[5]/li[1]/div[2]/div/p&quot;));

But i failed to catch text under tags with minor changes<br>
Target xpaths:

/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[6]/div[1]/ol[5]/li[1]/div[2]/div[4]/div[1]/div[1]/p[1]
/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[6]/div[1]/ol[5]/li[1]/div[2]/div[8]/p
/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[2]/div[1]/ol[5]/li[1]/div[2]/div[3]/h3[1]
/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[2]/div[1]/ol[5]/li[1]/div[2]/div[2]/div[1]/p
/html[1]/body[1]/div[2]/div[2]/dl[1]/dd[2]/div[2]/div[5]/div[1]/ol[5]/li[1]/div[2]/div[1]/div[1]/p[1]/strong[1]

What is correct formula or way to get all text content in the above mentioned xpaths ?

答案1

得分: 1

如果你想要获取包含直接文本的所有元素,你可以使用以下XPath表达式:

/html/body[1]//*[text()[normalize-space()]]

这将返回所有包含直接文本节点的元素,经过过滤不必要的空白字符后,保留字符数据。

XPath部分的含义如下:

  • // = 任何后代元素;参见这里有关轴的信息
  • * = 任何元素
  • [某个过滤条件] = 用于筛选直接前一个节点的谓词
  • [#number] = 在其同级元素中的位置。body[1]可能看起来多余,但可以帮助XPath引擎不再查找其他body元素
  • text() = 文本类型的节点
  • normalize-space() = 根据这些规则删除空白字符
英文:

Not very clear what you want: If you want all elements that contain direct text you could use:

/html/body[1]//*[text()[normalize-space()]]

this will return all elements with direct text()-nodes that after filtering unnecessary whitespace, have character-data.

meaning XPath-parts:

// = any descendant; see this info on axes

* = any element

[some filter] = predicate to filter on direct previous node.

[#number] = the position within its siblings. body[1] maybe seems redundant, but can help the XPath-engine not to look any further for other body elements

text() = node of type text

normalize-space() = strips white-space according this rules

huangapple
  • 本文由 发表于 2023年6月1日 18:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定