问题描述
有一个锚标记,用户可以更改其值。现在,我想编写一个Xpath查询,在一个语句本身中搜索多个链接文本名称。
<a href="#login" class="fancybox" xpath="1">Signin</a>
现在,可以通过用户配置文件更改链接文本值。例如“登录”,“登录”,“单击此处登录”或“立即登录”
如果我写xpath:-
//a[contains(text(),'Login') or contains(text(),'Log-in') or contains(text(),'Login now') or contains(text(),'click here to Login')]
然后失败。我需要使用Selenium单击此元素。
请帮助。
解决方法
重要说明:
- 仅在需要 substring 测试时使用
contains()
。参见What does contains() do in XPath? - 了解string-values:请参见Testing text() nodes vs string values in XPath
- 当心空白变化。参见What is the purpose of normalize-space()?
- 您发布的标记具有
Signin
,但您的XPath没有。 - 区分大小写:
click here to Login
与Click here to Login
不同。
XPath 1.0
如果确定没有空格变化,则
//a[.='Login' or .='Log-in' or .='Click here to login' or .='Login now']
否则:
//a[ normalize-space()='Login"
or normalize-space()='Log-in'
or normalize-space()='Click here to login'
or normalize-space()='Login now']
XPath 2.0
//a[normalize-space()=('Login','Log-in','Click here to login','Login now')]
,
XPath基于序列。因此,您可以使用逗号分隔的列表(即序列)来模仿逻辑OR条件。
XPath
//a[text()=("Login","Log-in","Click here to login")]