xpath中的点(.)如何在识别元素和匹配文本时采用多种形式 [英] How does dot(.) in xpath to take multiple form in identifying an element and matching a text

查看:26
本文介绍了xpath中的点(.)如何在识别元素和匹配文本时采用多种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 dom 结构:

I have the below dom structure:

<h3 class="popover-title">
 <div class="popup-title">
   <div class="title-txt">Associated Elements&nbsp;&nbsp(5)</div>
 </div>
</h3>

我正在尝试编写一个 xpath,它将识别 h3 标签下的标题关联元素".

I am trying to write an xpath which will identify the title "Associated Elements" under h3 tag.

当我的 xpath 是

When my xpath is

//div[contains(@class, popover)]//h3[contains(.,'Associated Elements')]

元素已被识别.

但是当我的 xpath 是

However when my xpath is

//div[contains(@class, popover)]//h3[contains(text(),'Associated Elements')]

元素未被识别.根据我的理解,dot(.) 是 text() 的替代品,但是为什么当我使用 text() 函数时它不能识别元素.

the element is not identified. As per my understanding the dot(.) is a replacement for text(), but then why does it not identify the element when I use the text() function.

但是,对于另一个 dom 结构:

However, for another dom structure:

<h3 class="popover-title">
   <a class="btn-popover" href="#">x</a>
   "Associated Elements"
</h3>

xpath:

//div[contains(@class, popover)]//h3[contains(text(),'Associated Elements')]

&

//div[contains(@class, popover)]//h3[contains(.,'Associated Elements')]

工作正常.

谁能解释一下 dot(.) 在这两种情况下的行为?

Can someone please explain the behaviour of dot(.) under both these scenarios?

有没有更好的方法来编写对两个示例都适用的 xpath?请提出建议.

Is there a better way to write an xpath that holds good for both the exmaples? Please suggest.

推荐答案

As 被标记所以这个答案将基于 和相关的 XML路径语言 (XPath) 版本 1.0 规范.

As selenium is tagged so this answer would be based on xpath-1.0 and the associated XML Path Language (XPath) Version 1.0 specifications.

函数boolean contains(string,string) 如果第一个参数字符串包含第二个参数字符串,则返回 true,否则返回 false.举个例子:

The function boolean contains(string, string) returns true if the first argument string contains the second argument string, and otherwise returns false. As an example:

//h3[contains(.,'Associated Elements')]

<小时>

文本节点

字符数据被分组到 文本节点.尽可能多的字符数据被分组到每个文本节点中.文本节点的 string-value 是字符数据.一个文本节点总是至少有一个字符的数据.在下面的示例中,text() 选择上下文节点的所有文本节点子节点:


Text Nodes

Character data is grouped into text nodes. As much character data as possible is grouped into each text node. The string-value of a text node is the character data. A text node always has at least one character of data. In the below example, text() selects all text node children of the context node:

//h3[text()='Associated Elements']

在您的用例中,在 HTML 中,文本 Associated Elements &nbsp(5) 具有 &nbsp; 或者简称固定空格硬空格NBSP(不间断空格)在编程中用来在一行中创建一个空格,不能被自动换行打破.在 HTML 中,&nbsp; 允许您创建多个在网页上可见的空间,而不仅仅是在源代码中.

In your usecase, within the HTML the text Associated Elements &nbsp(5) have &nbsp; which is alternatively referred to as a fixed space or hard space, NBSP (non-breaking space) used in programming to create a space in a line that cannot be broken by word wrap. Within HTML, &nbsp; allows you to create multiple spaces that are visible on a web page and not only in the source code.

您的第一次代码试用:

//h3[contains(.,'Associated Elements')]

在元素成功识别部分文本时定位元素关联元素

locates the element as it successfully identifies with partial text Associated Elements

您的第二次代码试用:

//h3[contains(text(),'Associated Elements')]

失败,因为元素包含更多字符,例如&nbsp; 除了文本Associated Elements.

fails as the element contains some more characters e.g. &nbsp; in addition to the text Associated Elements.

您可以在以下位置找到一些相关讨论:

You can find a couple of relevant discussions in:

这篇关于xpath中的点(.)如何在识别元素和匹配文本时采用多种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆