java Selenium Xpath 在 div 标签内查找表格单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32864336/
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
Selenium Xpath to find a table cells inside a div tag
提问by Calle
I have the following HTML code that I need to check if text exists within a table cell:
我有以下 HTML 代码,需要检查表格单元格中是否存在文本:
<div class="background-ljus" id="AutoText">
<table class="topAlignedCellContent">
<tbody>
<tr>
<td>X1</td>
</tr>
<tr>
<td>X2</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Y1</td>
<td>Y2</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>Z1</td>
<td>Z2</td>
</tr>
</tbody>
</table>
</div>
I have solved it like this:
我是这样解决的:
By locator = getLocator(CommonConst.XPATH, "*//div[@" + type + "='" + anyName + "']");
fluentWait(locator);
WebElement div = getDriver().findElement(locator);
List<WebElement> cells = div.findElements(By.tagName("td"));
for (WebElement cell : cells) {
if (cell.getText().contains(cellText)) {
foundit = true;
}
}
But i think its a bit slow because I need to do this several times. I tried to do this with only XPath but had no luck.
但我认为它有点慢,因为我需要这样做几次。我试图只用 XPath 做到这一点,但没有运气。
"*//div[@id='AutoText']//td[contains[Text(), 'celltext']]"
"*//div[@id='AutoText']//table//tbody//tr[td//Text()[contains[., 'celltext']]"
Anyone have a suggestion about why my XPath isn't working?
任何人都对为什么我的 XPath 不起作用有任何建议?
回答by Tomalak
Wrong:
错误的:
*//div[@id='AutoText']//td[contains[Text(), 'celltext']]
*//div[@id='AutoText']//table//tbody//tr[td//Text()[contains[., 'celltext']]
Correct (with shorter alternatives):
正确(使用较短的替代方案):
//div[@id='AutoText']//td[contains(text(), 'celltext')]
//div[@id='AutoText']//td[contains(., 'celltext')]
//div[@id='AutoText']/table/tbody/tr[td//text()[contains(., 'celltext')]
//div[@id='AutoText']/table/tbody/tr[td[contains(., 'celltext')]]
contains()
is a functiontext()
needs to be lowercase- predicates can be nested
- don't use
//
when you don't have to
contains()
是一个函数text()
需要小写- 谓词可以嵌套
//
非必要时不要使用
Note
笔记
.
refers to "this node" and, when given as an argument to a string function such as contains()
, is the equivalent of string(.)
. This, however, is not at all the same as text()
.
.
指的是“这个节点”,当作为字符串函数的参数给出时,例如contains()
,等价于string(.)
。然而,这与text()
.
string(.)
creates the concatenation of all text nodes inside the current node, no matter how deeply nested. text()
is a node testand by default selects only the direct children.
string(.)
创建当前节点内所有文本节点的串联,无论嵌套多深。text()
是一个节点测试,默认情况下只选择直接子节点。
In other words, if the current node was <td>foo <b>bar</b> baz</td>
, contains(text(), 'bar')
would actually be false. (So would contains(text(), 'baz')
, for a different reason.)
换句话说,如果当前节点是<td>foo <b>bar</b> baz</td>
,contains(text(), 'bar')
实际上是假的。(也是contains(text(), 'baz')
,出于不同的原因。)
contains(., 'bar')
on the other hand would return true, so would contains(., 'baz')
.
contains(., 'bar')
另一方面会返回真,所以会contains(., 'baz')
。
Only when the current node contains nothing but a single text node, text()
and .
are equivalent. Most of the time, you will want to work with .
instead of text()
. Set up your predicates accordingly.
仅当当前节点仅包含单个文本节点时,text()
并且.
是等效的。大多数时候,您会想要使用.
而不是text()
. 相应地设置你的谓词。
回答by Girish Sortur
Your xpath's are wrong. You should be using text()
instead of Text()
. Also contains()
is a function with round braces and not square braces. Try this -
你的 xpath 是错误的。您应该使用text()
而不是Text()
. 也是contains()
一个带有圆括号而不是方括号的函数。试试这个 -
"//div[@id='AutoText']//td[contains(text(), 'celltext')]"
"//div[@id='AutoText']//td[contains(., 'celltext')]"
Or instead if you want all the child elements with the text to be displayed then use .
instead of text()
或者,如果您希望显示所有带有文本的子元素,则使用.
代替text()
Hope it helps.
希望能帮助到你。
回答by Shubham Jain
You can do it with dynamic xpath as well
您也可以使用动态 xpath 来完成
//div[@id='AutoText']/table[1]//tbody/tr[1]/td
Now just replace 1 with i and use for loop to see if the element return any value or not
现在只需用 i 替换 1 并使用 for 循环查看元素是否返回任何值