简体   繁体   English

什么xpath 1表达式对多行文本进行不区分大小写的匹配?

[英]what xpath 1 expression to do case-insensitive matching of multiline text?

I have some html markup that contains many labels that look like this: 我有一些html标记,其中包含许多看起来像这样的标签:

<label>One two three four</label>
<label>One two three<br>four</label>

I'm trying to access the elements in a selenium test, based on the text string "One two three four". 我正在尝试基于文本字符串“一二三四”的硒测试中的元素。 I want to be able to match both of the above examples. 我希望能够匹配以上两个示例。

My first attempt at an xpath looks like this: 我对xpath的第一次尝试是这样的:

//label[text()[contains(., 'One two three four')]]

This works fine for labels that don't contain a <br> in the text, but fails to find the second instance that has a <br> . 对于文本中不包含<br>标签,这很好用,但是找不到具有<br>的第二个实例。

Doing a little research, it seems like normalize-space should work, but it doesn't seem to. 做一些研究,似乎规范化空间应该起作用,但是似乎没有。 This is the xpath I tried, which returns only the first node 这是我尝试过的xpath,它仅返回第一个节点

//label[text()[contains(normalize-space(.), 'One two three four')]]

I also tried this, but it returns zero nodes 我也试过了,但是返回零节点

//label[normalize-space(text())[contains(., 'Generate')]]

So, is there a way to craft an xpath that looks for a label with the text "One two three four", even if there is a <br> element in the actual html markup? 因此,即使实际的html标记中有一个<br>元素,有没有一种方法可以设计一个xpath来查找带有文本“ One two three four”的标签?

Or translate() away the spaces andthenmatchonthat: translate()删除空格,然后匹配:

$ cat foo.html 
<label>One two three four</label>
<label>One two three<br>four</label>
$ xpquery -p HTML '//label[contains(translate(normalize-space(.), " ", ""), "Onetwothreefour")]/text()' foo.html
One two three four
One two three
four
$ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM