Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count HTML child tag in Selenium WebDriver using Java

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

like image 982
Onu Avatar asked Dec 12 '13 20:12

Onu


2 Answers

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();
like image 200
Yi Zeng Avatar answered Sep 29 '22 08:09

Yi Zeng


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"))
like image 21
Michael Avatar answered Sep 29 '22 08:09

Michael