# Count Number of elements in list

**URL:** <https://forum.katalon.com/t/count-number-of-elements-in-list/10718>\
**Category:** Web Testing\
**Tags:** web-testing, katalon-studio\
**Created:** [May 6, 2018, 9:26am UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718 "2018-05-06T09:26:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukul\_jain](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mukul\_jain](https://forum.katalon.com/u/Mukul_jain)\
**Post date:** [May 6, 2018, 9:26am UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/1 "2018-05-06T09:26:22Z")

</div>

Hi all,

As seen the solution of oliver howard,

import org.openqa.selenium.By as By  
import org.openqa.selenium.WebDriver as WebDriver  
import org.openqa.selenium.WebElement as WebElement  
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

WebDriver driver = DriverFactory.getWebDriver()  
WebElement temp = driver.findElement(By.className(‘trip-cards’))  
List list = temp.findElement(By.xpath(‘.//li’))  
list.size()

This solution is good to work, the problem which i am getting is that list is counting the className as repetitive times i.e suppose there is a class ‘image’ and on that particular page there are 300 classes of that so it is sometimes counting it as 350 or 374, etc.

So, i used sets too i couldn’t find a solution, please help me out.

---

<div class="post-metadata">

**Author:** ![Tom\_Longhurst](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@Tom\_Longhurst](https://forum.katalon.com/u/Tom_Longhurst)\
**Post date:** [May 6, 2018, 10:58am UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/2 "2018-05-06T10:58:23Z")

</div>

Im not quite sure what you’re asking, but you basically want to tweak your xpath to only put elements into your list that you’re interested in I think.

The below would give you a list of all non invisible elements .

DriverFactory.getWebDriver().findElements(By.xpath("//_[contains(@class, ‘trip-cards’) and not(ancestor::_[contains(@style, ‘display: none’)])]/child::li")).size();

---

<div class="post-metadata">

**Author:** ![Mukul\_jain](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mukul\_jain](https://forum.katalon.com/u/Mukul_jain)\
**Post date:** [May 6, 2018, 3:12pm UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/3 "2018-05-06T15:12:45Z")

</div>

Yes, i am taking the xpath to put my elements into that list but the problem i am facing is that like i am having classname= imageView, and that classname is suppose having 300 times on that page, so when i put that xpath into the list its sometimes counting the xpath more than 300 times i.e taking one item as multiple times, so how can i solve it.

---

<div class="post-metadata">

**Author:** ![Mukul\_jain](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mukul\_jain](https://forum.katalon.com/u/Mukul_jain)\
**Post date:** [May 7, 2018, 5:07am UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/4 "2018-05-07T05:07:00Z")

</div>

Is there any solution??..i am stucked

---

<div class="post-metadata">

**Author:** ![Tom\_Longhurst](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@Tom\_Longhurst](https://forum.katalon.com/u/Tom_Longhurst)\
**Post date:** [May 7, 2018, 7:04am UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/5 "2018-05-07T07:04:47Z")

</div>

It won’t be taking one element multiple times. It’ll just be that class is used on lots of elements. Can you show us the DOM of the page? If we can see what you do want in your list and what you don’t want then that would make it easier

---

<div class="post-metadata">

**Author:** ![Mukul\_jain](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mukul\_jain](https://forum.katalon.com/u/Mukul_jain)\
**Post date:** [May 7, 2018, 12:43pm UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/6 "2018-05-07T12:43:56Z")

</div>

![](https://s3.amazonaws.com/katalon-forum/editor/bb/4xtx8wbpalgl.png)

//Count Listing

//WebDriver driver = DriverFactory.getWebDriver()

//List list = driver.findElements(By.className(‘imageView’))

[//list.size](https://list.size)()

//def countList=list.size()

//KeywordLogger printlog=new KeywordLogger()

[//printlog.logInfo](https://printlog.logInfo)(“( “+list.size().toString()+” PRODUCTS “+”)”)

So, this is the problem i am getting, i am counting the total class=‘imageView’ on this page, list is counting the elements but repeating the count of elements.

[Screenshot (63).png](https://s3.amazonaws.com/katalon-forum/editor/bb/4xtx8wbpalgl.png)

---

<div class="post-metadata">

**Author:** ![Tom\_Longhurst](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@Tom\_Longhurst](https://forum.katalon.com/u/Tom_Longhurst)\
**Post date:** [May 7, 2018, 1:24pm UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/7 "2018-05-07T13:24:39Z")

</div>

It seems fine for me. Not repeating anything. You could try using findElements by xpath instead.

List list = driver.findElements(By.xpath("//li[contains(@class, ‘imageView’)]"))  
or  
List list = driver.findElements(By.xpath("//ul[contains(@class, ‘prodBox’)]/child::li"))

Both of those returned 338 products on that page for me.

---

<div class="post-metadata">

**Author:** ![Mukul\_jain](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mukul\_jain](https://forum.katalon.com/u/Mukul_jain)\
**Post date:** [May 7, 2018, 2:31pm UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/8 "2018-05-07T14:31:35Z")

</div>

Thanks Tom it worked.  
I was taking the xpath wrong that’s why it was sometimes counting right, sometimes not.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![khaidir.kamil](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/khaidir.kamil/32/23870_2.png) [@khaidir.kamil](https://forum.katalon.com/u/khaidir.kamil)\
**Post date:** [March 5, 2019, 9:18am UTC](https://forum.katalon.com/t/count-number-of-elements-in-list/10718/9 "2019-03-05T09:18:51Z")

</div>

Hi, Im new to Katalon and groovy. Previously I use selenium + python but with very basic knowledge. So now my question is, how can I do this thing twice? Meaning let say, on top I want to check list product and below maybe I want to check list of blog post for example.

I cant write the list.list twice because it’s already call at the first list right. In python and selenium I cant do def xxx(self) but with Katalon, I dont know how to do it. Hope you guys can help.

Thanks
