[英]How to find matching length of matching elements having same xpath in selenium
I am new to Selenium and want to find the length of the nodes with matching xpath. 我是Selenium的新手,想找到具有匹配xpath的节点的长度。
eg: 例如:
<html>
<body>
<table>
<tbody>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
</tbody>
</table>
</body>
I want to write a java code in Selenium to find the length of the node. 我想在Selenium中编写一个java代码来查找节点的长度。 I want the length for example if i am finding the "tr" node i want it length to be 4
我想要长度,例如,如果我找到“ tr”节点,我希望长度为4
xpath will be .//html/body/table/tr xpath将是.//html/body/table/tr
Your xpath
is incorrect. 您的
xpath
不正确。 You should also use findElements
in plural to find more than one element. 您还应该使用复数形式的
findElements
来查找多个元素。 This will return List<WebElement>
and then you can simply call size()
to get the number of <tr>
tags. 这将返回
List<WebElement>
,然后您只需调用size()
来获取<tr>
标记的数量。 Try 尝试
int count = allocation.findElements(By.xpath("//html/body/table/tbody/tr")).size();
I gave absolute path since there aren't and other identifiers in the html you provided, but it isn't recommended. 我给出了绝对路径,因为你提供的html中没有和其他标识符,但不建议这样做。
In java, 在java中,
int rowCount=driver.findElements(By.xpath("//table[@id='DataTable01']/tbody/tr")).size();
int columnCount=driver.findElements(By.xpath("//table[@id='DataTable01']/tbody/tr/td")).size();
You can do it another way, 你可以用另一种方式
List<WebElement> rows = tblData.findElements(By.tagName("tr"));
int rowCount = rows.size();
You do not need to put //
if you are traversing from the very parent node. 如果您从父节点遍历,则不需要输入
//
。
Below xpath will work for you too xpath下面也适合你
/html/body/table/tbody/tr
Absolute XPath Absolute XPath starts with the root node or a forward slash (/). 绝对XPath绝对XPath以根节点或正斜杠(/)开头。 The advantage of using absolute is, it identifies the element very fast.
使用绝对的优点是,它可以非常快速地识别元素。 Disadvantage here is, if any thing goes wrong or some other tag added in between, then this path will no longer works.
这里的缺点是,如果出现任何问题或者其间添加了其他标记,那么此路径将不再有效。
Example: If the Path we defined as 示例:如果我们定义的路径为
1. html/head/body/table/tbody/tr/th
If there is a tag that has added between body and table as below 如果在正文和表格之间添加了一个标签,如下所示
2. html/head/body/form/table/tbody/tr/th
The first path will not work as 'form' tag added in between 第一条路径不能用作中间添加的“form”标记
Relative XPath 相对XPath
A relative XPath is one where the path starts from the node of your choise - it doesn't need to start from the root node. 相对的XPath是路径从选择的节点开始的路径-不需要从根节点开始。
It starts with Double forward slash( //
) 它以双正斜杠(
//
)开头
Syntax: 句法:
//table/tbody/tr/th
Advantage of using relative xpath is, you don't need to mention the long xpath, you can start from the middle or in between. 使用相对xpath的优点是,您无需提及长xpath,可以从中间或中间开始。
Disadvantage here is, it will take more time in identifying the element as we specify the partial path not (exact path). 这里的缺点是,识别元素需要更多时间,因为我们指定的部分路径不是(精确路径)。
If there are multiple elements for the same path, it will select the first element that is identified 如果同一路径有多个元素,它将选择被标识的第一个元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.