使用 XPATH 搜索包含空格的文本 [英] Using XPATH to search text containing whitespace
问题描述
在
<tr>
<td class="f4 trimJustl"
valign="middle"
style="color:#ffffff;"
artcolor="#ffffff">Create Network Container</td>
</tr>
我想找到文本等于 Create Network Container 的 td
元素.我创建了 XPath
I want to find the td
element where the text is equal to Create Network Container.
I created the XPath
//td[text()='Create Network Container']
但它不起作用.我也试过
But it's not working. I also tried
//td[contains(text(),'Create Network Container')]
但这对我也不起作用.
推荐答案
它通过剪切和粘贴您发布的示例来工作.您的原始来源可能包含不匹配的制表符或其他空白字符.以下是一些替代方案:
It works by cutting and pasting your posted example. Your original source probably has tabs or other whitespace characters that don't match. Here are some alternatives:
1) 规范化空格
//td[normalize-space(text()) = 'Create Network Container']
2) 使用 td
的字符串值进行比较(这也将匹配 Create Network Container
)
2) Compare using string value of td
(this will also match Create <b>Network</b> Container
)
//td[normalize-space(.) = 'Create Network Container']
3) 分别检查每个单词(忽略词序)
3) Check for each word separately (ignores word order)
//td[contains(text(), 'Create') and contains(text(), 'Network') and contains(text(), 'Container')]
4) 去除空格、制表符、换行符、换行符、回车和比较结果:
4) Strip whitespace, tabs, newlines, line-feeds, carriage returns and compare result:
//td[translate(text(), " 	
", "") = 'CreateNetworkContainer']
这篇关于使用 XPATH 搜索包含空格的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!