Skip to content Skip to sidebar Skip to footer

How To Count Html Child Tag In Selenium Webdriver Using Java

In Selenium JAVA WebDriver - How can I count child tags? Example:
Copy

Solution 2:

Here are two solutions:

You could either select by xpath

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

or you could select by CSS query as mentioned by user1177636

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

Solution 3:

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

In your case it will return "3" as result

Solution 4:

In general, I would use some way to find all of the elements that I want, either using xpath or css selectors, and then just count how many results are returned.

Solution 5:

Every element has an attribute called childElementCount. You can use WebElement.getAttribute() function to get an immediate value.

WebElementelement= driver.findElement(By.className("product_row"));
intnumberOfChilds= Integer.parseInt(element.getAttribute("childElementCount"));

Because getAttribute() returns a String, we have to cast it to an int.

Post a Comment for "How To Count Html Child Tag In Selenium Webdriver Using Java"