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

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

问题描述

我具有以下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标签下标识标题"Associated Elements".

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

当我的xpath是

//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.

推荐答案

作为和关联的 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')]


文本节点

字符数据被分组为文本节点.将尽可能多的字符数据分组到每个文本节点中.文本节点的字符串值是字符数据.文本节点始终至少具有一个字符数据.在下面的示例中,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 (5)具有 &nbsp; ,也称为固定的space 硬空间 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')]

在成功用部分文本 Associated Elements

您的第二个代码试用版,具有:

Your second code trial with:

//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:

  • How to locate the button element using Selenium through Python
  • What does contains(., 'some text') refers to within xpath used in Selenium
  • While fetching all links,Ignore logout link from the loop and continue navigation in selenium java

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

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