我是Selenium的新手,想找到匹配xpath的节点的长度。
例如:
<html> <body> <table> <tbody> <tr><td></td></tr> <tr><td></td></tr> <tr><td></td></tr> <tr><td></td></tr> </tbody> </table> </body>我想在Selenium中编写一个Java代码来查找节点的长度。 我想要的长度,例如,如果我找到“tr”节点,我希望它的长度是4
xpath将会是.//html/body/table/tr
I am new to Selenium and want to find the length of the nodes with matching 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. I want the length for example if i am finding the "tr" node i want it length to be 4
xpath will be .//html/body/table/tr
最满意答案
你的xpath不正确。 您还应该使用复数的findElements来查找多个元素。 这将返回List<WebElement> ,然后您可以简单地调用size()来获取<tr>标签的数量。 尝试
int count = allocation.findElements(By.xpath("//html/body/table/tbody/tr")).size();我提供了绝对路径,因为您提供的html中没有和其他标识符,但不推荐。
Your xpath is incorrect. You should also use findElements in plural to find more than one element. This will return List<WebElement> and then you can simply call size() to get the number of <tr> tags. 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.
更多推荐
发布评论