使用br从文本中查找xpath [英] Finding xpath from text with br

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

问题描述

我有这个html代码

<div>
  <span>test</span>
  foo
  <br />
  bar
</div>

我正在尝试直接在div内找到文本(因此是foo栏).

And I'm trying to find it with the text directly inside the div (so foo bar).

通常,我会去//div [normalize-space(text())='foobar'],但是由于
它不起作用.我试图添加空格或特殊字符,或修剪它们,但似乎无济于事.

Normally, I would go //div[normalize-space(text()) = 'foobar'], but because of the
it is not working. I tried to add spaces or special chars, or trim them, but nothing seem to work.

从测试中我看到了

//div/text() = foo bar
//div/text()[1] = foo
//div/text()[2] = bar
//div[text()[1] = foo] = the div
//div[text()[2] = bar] = nothing

似乎text()在返回值时仅返回索引[2],而不是在搜索值时返回.

It seem that text() return only a index[2] when returning a value and not for searching a value.

我使用java/selenium和firepath(用于测试xpath的firebug插件)测试了此代码.

I tested this code with java / selenium and firepath (a plugin of firebug to test xpath).

任何人都有关于如何从文本中获取div的想法,并且在可能的情况下不使用包含,因为它们不准确.

Anybody have an idea on how to get the div from the text, and if possible, without using contains because they are not accurate.

谢谢.

推荐答案

您可以尝试在下面的XPath表达式中使用

You can try to use below XPath expression to match required div by its text:

//div[normalize-space()="test foo bar"]

请注意,用于删除空白的函数实际上称为 normalize-space() ,但不是sanitize-space()

Note that function to remove white-spaces called actually normalize-space(), but not sanitize-space()

更新

如果您只想使用"foo""bar"文本节点,请尝试

If you want to use only "foo" and "bar" text nodes, then try

//div[concat(normalize-space(text()[2]), normalize-space(text()[3]))="foobar"]')

或者是否要单独使用它们

or if to use them separately

//div[normalize-space(text()[2])='foo' and normalize-space(text()[3])='bar']

这篇关于使用br从文本中查找xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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