使用 XPATH 搜索包含   的文本 [英] Using XPATH to search text containing  

查看:91
本文介绍了使用 XPATH 搜索包含   的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 XPather 浏览器在 HTML 页面上检查我的 XPATH 表达式.

I use XPather Browser to check my XPATH expressions on an HTML page.

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

My end goal is to use these expressions in Selenium for the testing of my user interfaces.

我得到了一个内容与此类似的 HTML 文件:

I got an HTML file with a content similar to this:

我想选择一个节点,其文本包含字符串 ".

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

使用像abc"这样的普通字符串没有问题.我使用类似于 //td[text()="abc"] 的 XPATH.

I want to select a node with a text containing the string "&nbsp;".

当我尝试使用像 //td[text()="&nbsp;"] 这样的 XPATH 时,它什么都不返回.是否有关于带有&"的文本的特殊规则?

With a normal string like "abc" there is no problem. I use an XPATH similar to //td[text()="abc"].

推荐答案

看来 OpenQA,后面的人Selenium 已经解决了这个问题.他们定义了一些变量来显式匹配空格.就我而言,我需要使用类似于 //td[text()="${nbsp}"] 的 XPATH.

解决方案

我在这里复制了 OpenQA 关于这个问题的文本(找到 此处):

It seems that OpenQA, guys behind Selenium, have already addressed this problem. They defined some variables to explicitely match whitespaces. In my case, I need to use an XPATH similar to //td[text()="${nbsp}"].

HTML 自动规范化元素内的空白,忽略前导/尾随空格和转换额外的空格、制表符和换行符单一空间.当 Selenium 读取文本时在页面外,它试图复制这种行为,这样你就可以忽略中的所有制表符和换行符你的 HTML 并根据文本在浏览器中的外观呈现.我们通过替换所有不可见的空白(包括不间断空格 "&nbsp;") 与单一空间.所有可见的换行符(

I reproduced here the text from OpenQA concerning this issue (found 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.

我们使用相同的归一化逻辑HTML Selenese 测试用例的文本表.这有许多好处.首先,你不需要查看页面的 HTML 源代码弄清楚你的断言应该是什么是;&nbsp;"符号不可见给最终用户,所以你不应该写作时不得不担心他们硒测试.(你不需要把测试用例中的&nbsp;"标记在包含的字段上 assertText"&nbsp;".) 你也可以把额外的Selenese 中的换行符和空格 标签;因为我们使用相同的测试用例的规范化逻辑正如我们对文本所做的那样,我们可以确保断言和提取的文本将完全匹配.

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.

这造成了一些问题那些罕见的场合,当你真的想要/需要插入额外的空格在您的测试用例中.例如,你可能需要在字段中键入文本,例如这个:foo".但如果你只是将 <td>foo </td> 写在你的Selenese 测试用例,我们将替换您的只有一个空格的额外空格.

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.

这个问题有一个简单的解决方法.我们在 Selenese 中定义了一个变量,${space},其值为单个空间.您可以使用 ${space}插入一个不会出现的空格自动修剪,像这样:foo${space}${space}${space}.我们还包含了一个变量${nbsp},可以用来插入一个不间断的空间.

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.

注意 XPaths 规范化按照我们的方式留白.如果你需要写一个 XPath 像//div[text()="hello world"] 但是链接的 HTML 是真的hello&nbsp;world",你需要插入一个真正的&nbsp;"到你的Selenese 测试用例以使其匹配,像这样://div[text()="hello${nbsp}world"].

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"].

这篇关于使用 XPATH 搜索包含 &amp;nbsp; 的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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