Java 如何在 webdriver 中使用 Xpath 通过精确文本匹配搜索节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19721111/
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
How to search node by exact text match using Xpath in webdriver
提问by souvik
I need a little help regarding searching an exact text using xpath in webDriver.
我需要一些关于在 webDriver 中使用 xpath 搜索精确文本的帮助。
Suppose i have the html as follows..
假设我有如下 html..
<html><body>
<table>
<tr>
<td><button>abcd</button></td>
<td><button>abc</button></td>
</tr>
</table>
</body></html>
Now i want to click button "abc"
现在我想点击按钮“abc”
I used xpath as //button[contains(text(),'abc')]
but it is always performing on button "abcd" as it also contain the text "abc". In this regards I need a predicate or some other procedure which can search exact text instead of containing text.
我使用 xpath as//button[contains(text(),'abc')]
但它总是在按钮“abcd”上执行,因为它也包含文本“abc”。在这方面,我需要一个谓词或其他一些可以搜索精确文本而不是包含文本的过程。
I also tried with //button[matches(text(),'abc')]
, //button[matches($string,'abc')]
, //button[Text='abc')]
, //button[.='abc')]
and many more but none of these worked out to identify "abc" button.
我也尝试过//button[matches(text(),'abc')]
, //button[matches($string,'abc')]
, //button[Text='abc')]
,//button[.='abc')]
等等,但没有一个能识别“abc”按钮。
I do not know if there is any problem regarding my xpath version as I'm not aware of the version. But I'm using java 1.6 JDK.
我不知道我的 xpath 版本是否有任何问题,因为我不知道版本。但我使用的是 java 1.6 JDK。
Though my exact scenario is not the example shown but similar logic needs to be applied.
虽然我的确切场景不是显示的示例,但需要应用类似的逻辑。
Hence any help or suggestion would be highly appreciated.
因此,任何帮助或建议将不胜感激。
回答by Akbar
Try with ends-with instead of contains. If the buttons dont have unique attributes, you can add the parent hierarchy as well. Like //table/tr/td[1].
尝试使用结尾而不是包含。如果按钮没有唯一属性,您也可以添加父层次结构。像 //table/tr/td[1]。
回答by olyv
I would use next xpath //button[text()='abc']
. You have mentioned text()
function but I'm not sure syntax was correct. Also you tried to use contains()
-- it searches partial text and WebDriver gets first element found. I your case it is <button>abcd</button>
button
我会使用下一个 xpath //button[text()='abc']
。您提到了text()
函数,但我不确定语法是否正确。您还尝试使用contains()
- 它搜索部分文本,WebDriver 获取找到的第一个元素。我你的情况是<button>abcd</button>
按钮
回答by nanosoft
//button[.="abc"]
The dot before the equality operator will do the text comparison. Another example is /PROJECT[.="MyProject"]
from the xPath Java tutorial.
相等运算符之前的点将进行文本比较。另一个示例/PROJECT[.="MyProject"]
来自xPath Java 教程。
回答by Priyanka Chaturvedi
To find the element 'abcd' you can simply use:
要查找元素“abcd”,您只需使用:
//button[contains(text(),'abcd')]
To find 'abc' use the normalize-space() function which will clean up your text for comparison purposes.
要查找“abc”,请使用 normalize-space() 函数,该函数将清理您的文本以进行比较。
//button[normalize-space(text())='abc']
回答by asadahm
For exact search:
精确搜索:
button[text()='abc']
For pattern matching search:
对于模式匹配搜索:
button[starts-with(.,'abc')]