简体   繁体   English

如何使用 Java 在 Selenium WebDriver 中计算 HTML 子标签

[英]How to count HTML child tag in Selenium WebDriver using Java

In Selenium JAVA WebDriver - How can I count child tags?在 Selenium JAVA WebDriver 中 - 如何计算子标签? Example:示例:

<div class="subcategory_container">
  <div class="products_container">
     <div class="product_row">
       <form class="product_container">
       <form class="product_container">
       <form class="product_container">
     </div>
   </div>
</div>

I want to count how many form tag are there under product_row div ?我想计算product_row div下有多少个表单标签? Thanks谢谢

You find the parent div first, then locate all target elements, then count them.你先找到父div,然后定位所有目标元素,然后统计它们。

List<WebElement> forms = driver.findElements(By.cssSelector(".product_row form"));
int count = forms.size();

Here are two solutions:这里有两个解决方案:

You could either select by xpath您可以通过 xpath 选择

driver.findElements(By.xpath("//div[@class='product_row']/form"))

or you could select by CSS query as mentioned by user1177636或者您可以通过user1177636提到的 CSS 查询进行选择

driver.findElements(By.cssSelector(".product_row>form"))

You can find the size of elements by using the following statement:您可以使用以下语句找到元素的大小:

System.out.println(driver.findElements(By.xpath("//div[@class='product_row']/form")).size());

Where .findElements method returns count value that all elements in that a page consists with xpath //div[@class='product_row']/form其中.findElements方法返回该页面中所有元素由 xpath //div[@class='product_row']/form组成的计数值

In your case it will return "3" as result在您的情况下,结果将返回“3”

通常,我会使用某种方法来查找我想要的所有元素,无论是使用 xpath 还是 css 选择器,然后只计算返回了多少结果。

Every element has an attribute called childElementCount.每个元素都有一个名为 childElementCount 的属性。 You can use WebElement.getAttribute() function to get an immediate value.您可以使用 WebElement.getAttribute() 函数来获取即时值。

WebElement element = driver.findElement(By.className("product_row"));
int numberOfChilds = Integer.parseInt(element.getAttribute("childElementCount"));

Because getAttribute() returns a String, we have to cast it to an int.因为 getAttribute() 返回一个 String,所以我们必须将它转换为一个 int。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java使用Selenium Webdriver在此html中获取标记值? - How to get the <b> tag value in this html with Selenium Webdriver using Java? 如何在Selenium Webdriver中使用Java验证HTML代码中是否存在用于下拉列表的标记 - How to verify whether a tag is present in the html code for a drop down using java in Selenium Webdriver 如何使用Selenium Webdriver和Java从href html标记中选择“ Page =?”值 - How to pick “Page= ?” valuse from href html tag using Selenium Webdriver and Java 如何<a class </a>在Selenium WebDriver中使用Java</a>单击带有标签名称的链接 - How to click a link with Tag name <a class </a> using Java in Selenium WebDriver 如何在java上使用selenium webdriver动态创建li标签 - How to create a li tag dynamically using selenium webdriver on java 如何使用Java关闭Selenium WebDriver中的子浏览器窗口 - How to close child browser window in Selenium WebDriver using Java 使用 java 在 selenium webdriver 中等待子 window - wait for child window in selenium webdriver using java 如何使用Selenium WebDriver和Java根据HTML单击复选框 - How to click on the checkbox as per the HTML using Selenium WebDriver and Java 如何通过与Java一起使用Selenium WebDriver从此HTML代码中获取文本 - How to get Text from this HTML code By using Selenium WebDriver with Java 使用Java的Selenium WebDriver和HTML Window位置 - Selenium WebDriver and HTML Window location by using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM