Html 使用链接文本查找链接的 xpath?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41586008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding xpath of a link using link text?
提问by user2416212
In Selenium WebDriver, how to use xpath for the below HTML using Link Text ("Add New Button")
在 Selenium WebDriver 中,如何使用链接文本(“添加新按钮”)为以下 HTML 使用 xpath
<a href="SOME URL">
<span >
<i class=""/>
</span>Add New Button
</a>
I tried to inspect the element as below but all dint work
我试图检查元素如下,但所有的工作
1) //a[text()='Add New Button']
2) //a[contains(text(),'Add New Button']
3) //a/span/i[text()='Add New Button']
4) //a/span/i[contains(text(),'Add New Button']
I know that 3 and 4 won't , but just tried it.
我知道 3 和 4 不会,但只是尝试过。
So for such a HTML DOM, How to find the link using the link text using xpath?
那么对于这样的 HTML DOM,如何使用 xpath 使用链接文本查找链接?
回答by Mathias Müller
Some of the answers that were already given work, others don't. And I think the OP would profit from more explanations.
一些已经给出的答案有效,另一些则没有。而且我认为 OP 会从更多的解释中受益。
Your original expression:
你的原始表达:
//a[text()='Add New Button']
Does not work because the text node that contains "Add New Button" also has a newline character at the end.
不起作用,因为包含“添加新按钮”的文本节点在末尾也有一个换行符。
The next one:
下一个:
//a[contains(text(),'Add New Button')]
Does not work (leaving aside the missing parenthesis) because text()
returns a sequenceof nodes and a function like contains()
will only evaluate the firstnode in the sequence. In this case, the first text node inside a
only contains whitespace and is notthe one that contains "Add New Button".
不起作用(将缺少的括号放在一边),因为text()
返回一个节点序列和一个函数 likecontains()
只会评估序列中的第一个节点。在这种情况下,内部的第一个文本节点a
仅包含空格,而不是包含“添加新按钮”的文本节点。
You can validate this claim with:
您可以通过以下方式验证此声明:
//a[contains(text()[2],'Add New Button')]
which will test whether the secondtext node of a
contains "Add New Button" - and this expression will return the a
element. But the best solution in this case is:
这将测试第二个文本节点是否a
包含“添加新按钮” - 此表达式将返回该a
元素。但在这种情况下最好的解决方案是:
//a[contains(.,'Add New Button')]
.
will evaluate to the so-called "string value" of an element, a concatenation of all its text nodes which will include "Add New Button".
.
将评估为元素的所谓“字符串值”,即包含“添加新按钮”的所有文本节点的串联。
A solution with normalize-space()
is also possible, but has nested predicates:
一个解决方案normalize-space()
也是可能的,但有嵌套的谓词:
//a[text()[normalize-space(.) = "Add New Button"]]
回答by lordkain
with xpath you can check if the element contains a text with below statement
使用 xpath,您可以检查元素是否包含带有以下语句的文本
//a[contains(., 'Button')]
回答by Andersson
Link text contains unnecessary spaces from the right side of main text, so you need following to get rid of them:
链接文本包含正文右侧不必要的空格,因此您需要按照以下步骤删除它们:
'//a[normalize-space(.)="Add New Button"]'
回答by NarendraR
Use following xpath
使用以下 xpath
//*[contains(text(),'Add New Button')]
or
或者
//a/i[contains(text(),'Add New Button')]
or
或者
//a[@href='SOME URL']/i
or Using cssSelector
-
或使用cssSelector
-
a[href='SOME URL']>i