如何找到硒中具有相同xpath的匹配元素的匹配长度(How to find matching length of matching elements having same xpath in sele

编程入门 行业动态 更新时间:2024-10-20 16:33:22
如何找到硒中具有相同xpath的匹配元素的匹配长度(How to find matching length of matching elements having same xpath in selenium) java

我是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.

更多推荐

本文发布于:2023-07-17 13:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144958.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长度   元素   find   xpath   elements

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!