# How to find the total number of rows in a nested table using xpath?

**URL:** <https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [February 22, 2022, 7:53pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783 "2022-02-22T19:53:59Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 24, 2022, 8:59am UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/21 "2022-02-24T08:59:46Z")

</div>

I made a github project. You can download a zip of a sample project from [here](https://github.com/kazurayam/KS_Keyword_openBrowser_with_local_file/releases).

> **[GitHub - kazurayam/KS\_Keyword\_openBrowser\_with\_local\_file](https://github.com/kazurayam/KS_Keyword_openBrowser_with_local_file)**
>
> Contribute to kazurayam/KS\_Keyword\_openBrowser\_with\_local\_file development by creating an account on GitHub.

The project contains the MHTML @1Medy_Albion provided at a folder `./tmp/Events.mhtml`

It includes a Test Case `Test Case/1Medy_Albion`, which looks like this:

```auto
import org.openqa.selenium.WebElement

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

CustomKeywords."ks.should.have.such.builtin.Keyword.openBrowserWithFile"("tmp/Events.mhtml")

WebUI.switchToFrame(byXPath("//iframe[@id='PopDiv']"), 10)
WebUI.verifyElementPresent(byXPath("//table[@id='mlist']"), 10)
List<WebElement> tableRows = WebUI.findWebElements(byXPath("//table[@id='mlist']/tbody/tr"), 10) 
assert tableRows != null
WebUI.comment("number of rows of <table id='mlist'> : " + tableRows.size())
WebUI.closeBrowser()

TestObject byXPath(String xpath) {
	TestObject tObj = new TestObject(xpath)
	tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
	return tObj
}

```

You want to execute it using Chrome browser.

This test case opens the local MHTML file in Chrome, switch to the `<iframe>`, find the `<table>`, select the `<tr>` set.

This emits the following output in the console.

```auto
2022-02-24 17:59:09.326 INFO c.k.k.c.keyword.builtin.CommentKeyword - number of rows of <table id='mlist'> : 15

```

Your question has been resolved.

OK?

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 10:47am UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/22 "2022-02-24T10:47:42Z")

</div>

Yes. The row\_count is 15.  
@kazurayam I am beyond grateful for your endeavour!

I am doing the merging now and also trying to understand this part:

```auto
TestObject byXPath(String xpath) {
	TestObject tObj = new TestObject(xpath)
	tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
	return tObj
}

```

If I have questions, surely I will ask you.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 24, 2022, 11:36am UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/23 "2022-02-24T11:36:51Z")

</div>

@sara.leslie  
@Russ_Thomas

I think I could demonstrate how a MHTML makes it easier to solve a forum topic.

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 12:01pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/24 "2022-02-24T12:01:00Z")

</div>

Correct Output  
 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/f/7/f7b4593c5b23cf9e2ecf0753655081276316e53f.png)

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [February 24, 2022, 1:51pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/25 "2022-02-24T13:51:54Z")

</div>

> [@kazurayam](#):
>
> @sara.leslie  
> @Russ_Thomas
> 
> I think I could demonstrate how a MHTML makes it easier to solve a forum topic.

Slightly better English: “I think I demonstrated how a MHTML makes it easier to solve a forum topic.”

I agree, you certainly did. My only problem is, I do not have the kind of time to spend on it that you have. ☹

And you surely know, had I been involved, I’d have done it with CSS selectors in a snap.

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 2:12pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/26 "2022-02-24T14:12:50Z")

</div>

@kazurayam Having got the total number of rows as 15, I want to go to the 3rd column (APP ACC), iterate the column and search for “I”.

Codes:

```auto
for (def rowIndex=1; rowIndex<=15; rowIndex++) {
def theName = WebUI.getText(findTestObject(By.xpath("//table[@id='mlist']/tbody/tr/td[3]", ['rowIndex' : rowIndex])))
println theName

	if (theName.equals("I")) {
		String AppText=WebUI.getText(findTestObject('Object Repository/Reported By/ApplicationAccount2'))
		println (AppText)
		break
	}
else {
	println ("Name is not in the first page")
	}
}

```

The problem is that the following event FAILED:

```auto
theName = getText(findTestObject(by.xpath("//table[@id='mlist']/tbody/tr/td[3]", ["rowIndex":rowIndex])))

```

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/2/9/2969a7b3aec8896242dbd5d5b39a41108e4ab508.png)

I am not able to figure out the script 1643367625129. What is it actually?

Also, why xpath is being underlined?

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/3/2/32b13e7cbd45de6ca70c08fb2b28ee963ecd472d.png)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [February 24, 2022, 4:21pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/27 "2022-02-24T16:21:24Z")

</div>

> [@1Medy\_Albion](#):
>
> ```auto
> def theName = WebUI.getText(findTestObject(By.xpath("//table[@id='mlist']/tbody/tr/td[3]", ['rowIndex' : rowIndex])))
> 
> ```

How about the below?

```auto
    def theName = WebUI.getText(findTestObject(By.xpath("//table[@id='mlist']/tbody/tr[${rowIndex}]/td[3]")))

```

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 5:09pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/28 "2022-02-24T17:09:12Z")

</div>

@grylion54 Ah I got the point. We keep col 3 fixed while the rows keeps changing.  
After execution with your new code, it is giving this result:

2022-02-24 21:03:56.106 DEBUG testcase.Create Event - 1:

```auto
 theName = getText(findTestObject(By.xpath(//table[@id='mlist']/tbody/tr[$rowIndex]/td[3])))

```

Reason:  
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.testobject.ObjectRepository.findTestObject() is applicable for argument types: (org.openqa.selenium.By$ByXPath) values: [By.xpath: //table[@id=‘mlist’]/tbody/tr[1]/td[3]]

Possible solutions: findTestObject(java.lang.String), findTestObject(java.lang.String, java.util.Map)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [February 24, 2022, 5:14pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/29 "2022-02-24T17:14:32Z")

</div>

Okay. I just went with your code. How about the below?

```auto
def theName = driver.findElement(By.xpath("//table[@id='mlist']/tbody/tr[${rowIndex}]/td[3]")).getText()

```

or

```auto
def myCell = driver.findElement(By.xpath("//table[@id='mlist']/tbody/tr[${rowIndex}]/td[3]"));
def theName = myCell.getText();

```

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 5:17pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/30 "2022-02-24T17:17:53Z")

</div>

Ok. I try the new one.  
To retrieve the names, i think it’s better to use the xpath:  
//\*[@id=“mlist”]/tbody/tr[1]/td[3]/div/span/text()

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/4/5/4505011ba06924a4afaf174ad38468229fde0447.png)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [February 24, 2022, 5:22pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/31 "2022-02-24T17:22:34Z")

</div>

No. The xpath you show is not valid.  
`//*[@id="mlist"]/tbody/tr[1]/td[3]/div/span/text()`

Split it in two. Use the xpath, `id("mlist")/tbody/tr[1]/td[3]/div/span` to get the element, and then use **getText()** of the element.

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 5:30pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/32 "2022-02-24T17:30:51Z")

</div>

It’s KO with the result:

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/7/4/744713567fa260993e59fb8bc7313e611cb3acdd.png)

And, katalon is underlining these words:

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/f/0/f0fd1a4a8b5294e3f128858f9663fa5169360983.png)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [February 24, 2022, 5:32pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/33 "2022-02-24T17:32:59Z")

</div>

You need the following import statements at the top of your Test Case. If you select CTRL + SHIFT + O (oh) at the same time, the imports at the top will generally be filled in (or reduced if you have some that are not needed by your code).

```auto
import org.openqa.selenium.By as By
import org.openqa.selenium.Keys as Keys
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement

import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

```

And after you have opened your browser…

```auto
WebDriver driver = DriverFactory.getWebDriver();

```

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [February 24, 2022, 5:36pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/34 "2022-02-24T17:36:56Z")

</div>

> [@grylion54](#):
>
> ```auto
> By.xpath('id("mlist")/tbody/tr[1]/td[3]/div/span'));
> 
> ```

Also, if you go with the xpath format I have above, you do not need the starting `//`. Remove them.

Or go back to the old format. For the **id** attribute, these are equivalent.

```auto
By.xpath('//*[@id="mlist"]/tbody/tr[1]/td[3]/div/span'))

```

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 5:46pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/35 "2022-02-24T17:46:00Z")

</div>

The imports were well present.  
Still giving error with new xpath.

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/8/0/80f2fa16f803388eb44cd1fc92d14cbfa6a1b03b.png)

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [February 24, 2022, 5:46pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/36 "2022-02-24T17:46:42Z")

</div>

See my amendment to the import list.

Make sure you put the statement **outside** of your **for** loop. You do not want to call it more than once.

I also note that you have hardcoded the **tr** attribute value to 1. To review all rows, this needs the loop counter as a parameter as I showed you above. If you leave it as hardcoded, you do **not** need the loop at all.

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 5:54pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/37 "2022-02-24T17:54:44Z")

</div>

Yep. I wanted to see if it retrieves the first name. I change it.  
You know this table is in an iframe. Where should i put the statement

```auto
WebDriver driver = DriverFactory.getWebDriver();

```

after the switch or just above the for loop?

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [February 24, 2022, 6:11pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/38 "2022-02-24T18:11:24Z")

</div>

It doesn’t matter. It’s just a declaration to shorten “DriverFactory.getWebDriver()” to just “driver” so we don’t have to type out the whole phrase again and again. It just has to be above the first **usage** of the reference “driver” on the Test Case.

---

<div class="post-metadata">

**Author:** ![1Medy\_Albion](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/1medy_albion/32/16104_2.png) [@1Medy\_Albion](https://forum.katalon.com/u/1Medy_Albion)\
**Post date:** [February 24, 2022, 6:26pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/39 "2022-02-24T18:26:54Z")

</div>

I agree. Do you think i need to format the XML to solve the issue?  
Reason:  
groovy.lang.MissingPropertyException: No such property: driver for class: Script1643367625129

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [February 24, 2022, 6:32pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/40 "2022-02-24T18:32:04Z")

</div>

Can you post your code you have so far?

[Previous page](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783.md?page=1)

[Next page](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783.md?page=3)
