In Selenium JAVA WebDriver - How can I count child tags? 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? Thanks
You find the parent div first, then locate all target elements, then count them.
List<WebElement> forms = driver.findElements(By.cssSelector(".product_row form"));
int count = forms.size();
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With