xml – 使用XPATH搜索包含的文本

我使用 XPather Browser在HTML页面上检查我的XPATH表达式。

我的最终目标是在Selenium中使用这些表达式来测试我的用户界面。

我得到了一个内容类似于这样的HTML文件:

<tr>
  <td>abc</td>
  <td>&nbsp;</td>
</tr>

我想选择一个文本包含字符串“& nbsp;”的节点。

用一个正常的字符串像“abc”没有问题。我使用类似于// td [text()=“abc”]的XPATH。

当我尝试使用像// td [text()=“& nbsp;”]这样的XPATH时,它不返回任何内容。有关于“&”的文本的特殊规则?

似乎 OpenQA,家伙背后Selenium,已经解决了这个问题。他们定义了一些变量来显式地匹配空格。在我的情况下,我需要使用类似于// td [text()=“$ {nbsp}”]的XPATH。

我在这里转载了OpenQA关于这个问题的文本(发现here):

HTML automatically normalizes
whitespace within elements,ignoring
leading/trailing spaces and converting
extra spaces,tabs and newlines into a
single space. When Selenium reads text
out of the page,it attempts to
duplicate this behavior,so you can
ignore all the tabs and newlines in
your HTML and do assertions based on
how the text looks in the browser when
rendered. We do this by replacing all
non-visible whitespace (including the
non-breaking space “&nbsp;“) with a
single space. All visible newlines
(<br>,<p>,and <pre> formatted
new lines) should be preserved.

We use the same normalization logic on
the text of HTML Selenese test case
tables. This has a number of
advantages. First,you don’t need to
look at the HTML source of the page to
figure out what your assertions should
be; “&nbsp;” symbols are invisible
to the end user,and so you shouldn’t
have to worry about them when writing
Selenese tests. (You don’t need to put
&nbsp;” markers in your test case
to assertText on a field that contains
&nbsp;“.) You may also put extra
newlines and spaces in your Selenese
<td> tags; since we use the same
normalization logic on the test case
as we do on the text,we can ensure
that assertions and the extracted text
will match exactly.

This creates a bit of a problem on
those rare occasions when you really
want/need to insert extra whitespace
in your test case. For example,you
may need to type text in a field like
this: “foo “. But if you simply
write <td>foo </td> in your
Selenese test case,we’ll replace your
extra spaces with just one space.

This problem has a simple workaround.
We’ve defined a variable in Selenese,
${space},whose value is a single
space. You can use ${space} to
insert a space that won’t be
automatically trimmed,like this:
<td>foo${space}${space}${space}</td>.
We’ve also included a variable
${nbsp},that you can use to insert
a non-breaking space.

Note that XPaths do not normalize
whitespace the way we do. If you need
to write an XPath like
//div[text()="hello world"] but the
HTML of the link is really
hello&nbsp;world“,you’ll need to
insert a real “&nbsp;” into your
Selenese test case to get it to match,
like this:
//div[text()="hello${nbsp}world"].

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇