简体   繁体   English

如何在硒中找到具有相同xpath的匹配元素的匹配长度

[英]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中,

For counting row size, 用于计算行大小,

int rowCount=driver.findElements(By.xpath("//table[@id='DataTable01']/tbody/tr")).size();

For counting column 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.

相关问题 即使所有匹配节点的xpath相同,如何识别硒中的元素 - How to identify an element in selenium even if xpath is same for all the matching nodes 定位器有 4 个匹配节点,当我们有 4 个元素匹配时,如何在 Selenium 中编写 xpath? - Locators has 4 matching nodes, how to write xpath in Selenium when we have 4 elements matching? Selenium java,在xpath匹配多个元素的情况下通过xpath等待元素和匹配文本 - Selenium java, wait for element by xpath and matching text in case of xpath matching multiple elements xpath如何选择所有匹配的元素 - xpath how to choose all elements matching 如何在 selenium java 中查找具有相同 CSS 选择器的元素数量? - How to find the number of elements having the same CSS selector in selenium java? Selenium:从数组中的字符串匹配xpath - Selenium: Matching xpath from strings in an array 通过Selenium中的xpath定位器匹配div的问题 - Problems matching div via xpath locators in Selenium 如何在.xpath中找到Selenium Web Driver中的元素 - How to find elements in Selenium Web Driver by.xpath 两个字符串中长度为2(在相同索引处)的匹配子序列 - Matching subsequence of length 2 (at same index) in two strings Selenium geckodriver无法找到匹配的功能集 - Selenium geckodriver unable to find a matching set of capabilities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM