# 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:** 19\
**Page:** 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, 6:39pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/41 "2022-02-24T18:39:09Z")

</div>

Yes. It clicks on a search icon and then an iframe opens up containing a table.

```auto
WebUI.click(findTestObject('Object Repository/Create Event/SearchReportedBy'))

WebUI.delay(3)

if (WebUI.switchToFrame(findTestObject('Object Repository/Reported By/iFrame_ReportedBy'), 5, FailureHandling.OPTIONAL)) {
	println (" switchToFrame is true ")
	}
	else {
	println (" switchToFrame is false")
}

//Click on alphabet B 
WebUI.click(findTestObject('Object Repository/Reported By/Filter'))

WebDriver driver = DriverFactory.getWebDriver()
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())

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

for (def rowIndex=1; rowIndex<=15; rowIndex++) {

def myCell = driver.findElement(By.xpath('//*[@id="mlist"]/tbody/tr[${rowIndex}]/td[3]/div/span'))
	def theName = myCell.getText()
	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")
	}
}

```

Please note that it cannot click on alphabet B for the time being. Another issue. Now, I am just running the script at the start of 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:52pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/42 "2022-02-24T18:52:05Z")

</div>

> [@1Medy\_Albion](#):
>
> ```auto
> WebUI.verifyElementPresent( **byXPath** ("//table[@id='mlist']"), 10)
> 
> TestObject **byXPath** (String xpath) { 
> TestObject tObj = new TestObject(xpath) 
> tObj.addProperty("xpath", ConditionType.EQUALS, xpath) 
> return tObj 
> }
> 
> ```

Just a bit of an explanation. The “byXPath” in your code above is a method that allows you to **create** a Test Object by supplying an xpath, so we don’t need the “driver” reference. How about the below:

```auto
for (def rowIndex=1; rowIndex<=15; rowIndex++) {

   def myCell = byXPath("//*[@id='mlist']/tbody/tr[${rowIndex}]/td[3]/div/span")
   def theName = WebUI.getText(myCell)
   println theName

```

---

<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, 7:07pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/43 "2022-02-24T19:07:46Z")

</div>

Oh I understand. You mean the function byXPath creates at object virtually at runtime?

---

<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, 7:13pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/44 "2022-02-24T19:13:56Z")

</div>

The outcomes:

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/1/a/1aea7690f428c3c1614749053695ca63ed7b83bf.png)

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

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/5/6/56b1c1291c320ac4a99a1ac61752914acf97ff8e.png)

Which properties is it referring to?

In dev mode  
//\*[@id=‘mlist’]/tbody/tr[${rowIndex}]/td[3]/div/span works fine.

---

<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, 7:18pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/45 "2022-02-24T19:18:42Z")

</div>

The “properties” is the pathway (the xpath) provided to the byXPath method.

Does the pathway **`"//*[@id='mlist']/tbody/tr[1]/td[3]/div/span"`** exist? The upper error message states that the pathway has a problem. Are you trying on two different environments? Are the builds the same?

---

<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, 7:21pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/46 "2022-02-24T19:21:44Z")

</div>

Oh no! The xpath does not point to any object on the true env.

This one //\*[@id=“mlist”]/tbody/tr[1]/td[3] works. I replace and see what it will give.

The xpath is good.

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

But again KO.

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/0/5/055b4fdcc0278d2085af0b13b13af283d1e524f8.png)  
 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/f/a/faf08b8dbfcae07697209ae562b847afcc28a620.png)  
Which properties?

---

<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, 8:14pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/47 "2022-02-24T20:14:40Z")

</div>

Is there another `<iframe>` to be concerned about?

---

<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, 8:29pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/48 "2022-02-24T20:29:23Z")

</div>

I see there are 2 actually.  
@grylion54 @kazurayam I can NOW upload a duplicate of the true environment!

[testFindNameAndID.zip](https://forum.katalon.com/uploads/short-url/rbK0sYIR9DM9E1frIKSLBXLcaNc.zip) (1.6 MB)

How to retrieve an APPLICATION ACCOUNT (for eg. Fadela Haddadi {Main}) and capture his EMPLOYEE ID?

---

<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, 9:12pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/49 "2022-02-24T21:12:18Z")

</div>

You already know how to loop to find one item (like the name), such as “Fadela Haddadi {Main}” and you were able to do that with a “parameterized” **rowIndex**. So the rowIndex of the “Employee Id” would be the exact same, but the column, or “td” element, would be different–and you know which column it is (you can hardcode this column). There you go. Put the two concepts together.

Maybe like:

```auto
   def myCell = byXPath("//*[@id='mlist']/tbody/tr[${rowIndex}]/td[3]")
   def theName = WebUI.getText(myCell)

   if (theName.contains("Fadela Haddadi")) { // or (theName == "Fadela Haddadi {Main}")
          def myEmpId = byXPath("//*[@id='mlist']/tbody/tr[${rowIndex}]/td[???]")
          def theEmpid = WebUI.getText(myEmpId )

    }

```

---

<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 25, 2022, 5:15am UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/50 "2022-02-25T05:15:19Z")

</div>

Ok. Thanks.  
The xpath “//\*[@id=‘mlist’]/tbody/tr[${rowIndex}]/td[3]/div/span” does not work.

//\*[@id=‘mlist’]/tbody/tr[${rowIndex}]/td[3] → Works.

EMP ID is the 2nd col.  
xpath → “//\*[@id=‘mlist’]/tbody/tr[${rowIndex}]/td[2]”

The test fails at the step:

```auto
def myCell = byXPath("//*[@id='mlist']/tbody/tr[${rowIndex}]/td[3]")

```

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

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

@grylion54 Do you have any idea of the properties it is mentioning?

---

<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 25, 2022, 6:27pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/51 "2022-02-25T18:27:25Z")

</div>

> [@How to find the total number of rows in a nested table using xpath?](http://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/45):
>
> The “properties” is the pathway (the xpath) provided to the byXPath method. Does the pathway "//\*[@id='mlist']/tbody/tr[1]/td[3]/div/span" exist? The upper error message states that the pathway has a problem. Are you trying on two different environments? Are the builds the same?

---

<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 25, 2022, 7:56pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/52 "2022-02-25T19:56:44Z")

</div>

@grylion54 I replied in the previous post. The pathway “//\*[@id=‘mlist’]/tbody/tr[1]/td[3]/div/span” does not work.

//\*[@id=‘mlist’]/tbody/tr[${rowIndex}]/td[3] \>\>\>Works.

I was using two different environments. But now I am using the TRUE build which I uploaded yesterday - please see post #47.

All logic is good. There are no errors in the script. I ran it many times today. Yet, the error is persistant.

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

What can we do to tackle this issue? Can you please download the true build and see if you are getting the names and it gets the expected name, let’s say Fadela Haddadi {Main} ?

---

<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 25, 2022, 11:21pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/53 "2022-02-25T23:21:03Z")

</div>

> [@1Medy\_Albion](#):
>
> I was using two different environments. But now I am using the TRUE build

You wrote you are using two environments. I guess, one is a development environment, which has small amount of data, which responds quickly. Another environment is a production environment, which has full of data, which responds slowly (you need to wait for a few seconds). Your script happened to run OK against the dev env, but it fails against the prod env. Why?

This is a Frequently Asked Question in this forum.

Any web app needs certain duration of milli-seconds to respond. Web app responds slower than your test script runs. Especially, the document inside `<iframe>` would require several seconds to load because it is loaded from another URL.

Therefore your script **must cope with the slowness of your Application Under Test**. Your test script must wait for the HTML pages are completely loaded into browser before it makes any action against the page.

You need to lear how to use “WebUI.verifyXXXX(TestObject, timeout)” and “WebUI.waitForXXXX(TestObject, timeout)” keywords. For example

[https://docs.katalon.com/katalon-studio/docs/webui-verify-element-present.html](https://docs.katalon.com/katalon-studio/docs/webui-verify-element-present.html)

Search this forum for previous topics that tell you how to use these keywords. You will easily find many.

---

<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 26, 2022, 2:13pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/54 "2022-02-26T14:13:37Z")

</div>

@kazurayam Yes I did researches. I used the function WebUI.verifyElementPresent.

Test Code

```auto
WebUI.delay(5)
for (def rowIndex=1; rowIndex<=15; rowIndex++) {
WebUI.verifyElementPresent(byXPath("//[@id='mlist']/tbody/tr[${rowIndex}]/td[3]"), 20)	
def myCell = byXPath("//*[@id='mlist']/tbody/tr[${rowIndex}]/td[3]")
def theName = WebUI.getText(myCell)
println theName
	
if (theName.contains("Scott Backhouse {Main}")) {  
def myEmpId = byXPath("//*[@id='mlist']/tbody/tr[${rowIndex}]/td[2]")
def theEmpid = WebUI.getText(myEmpId)
}
else {
println ("Name is not in the first page")
	}
}

```

This is what it is giving:  
I am using the Chrome browser. Why is it showing Netscape?

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

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/5/7/57f7574256bcb798e1e7cd89bdbb9b754be00f0f.png)  
 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/7/9/791df832c29381662bbe48d2d9ad07cba4ff4d63.png)  
The name is well present in the page but it is unable to find it!

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/8/9/89336295a3c09b915d43c2d0d05b548c3b9b8138.png)

@grylion54 @kazurayam Any other way of getting the name?

---

<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 26, 2022, 5:23pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/55 "2022-02-26T17:23:28Z")

</div>

> [@1Medy\_Albion](#):
>
> if (theName.contains(“Scott Backhouse {Main}”)) {

The idea of “contains” is to get a **smaller** bit of the whole. If you are going to compare to the whole, then perhaps use:

```auto
if (theName == "Scott Backhouse {Main}") {

```

Saying that, there are a lot of ways that you can use “contains”, like:

```auto
if (theName.contains("Scott Backhouse")) {

```

Also, since you are having concerns about finding the name, you should make a **print** out of the names so you can see if there is something else in the “cell” that you have not realized, such as a non-printable character, including getting the correct column

```auto
println("Name is ${theName}")
if (theName.contains("Scott Backhouse")) {

```

---

<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 26, 2022, 6:51pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/56 "2022-02-26T18:51:03Z")

</div>

Ah Ok. But it is still KO.

It is failing here:

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

 ![image](https://europe1.discourse-cdn.com/katalon/original/3X/0/0/0039f7f1ad81f6daae42a441509bc44dd3e6030c.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 26, 2022, 8:01pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/57 "2022-02-26T20:01:15Z")

</div>

How about slowing your script down with a **waitFor** statement?

> [@kazurayam](#):
>
> ```auto
> for (def rowIndex = 0; rowIndex < 15; rowIndex++) {
> WebUI.waitForElementVisible(byXPath("//[@id='mlist']/tbody/tr[${rowIndex}]/td[3]"), 10)
> WebUI.verifyElementPresent(byXPath("//[@id='mlist']/tbody/tr[${rowIndex}]/td[3]"), 20)	
> 
> ```

I also notice that you **are** printing out the name but nothing is displayed. You should find out why. Is your path incorrect or is there another reason.

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

---

<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 26, 2022, 10:59pm UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/58 "2022-02-26T22:59:55Z")

</div>

@1Medy_Albion

Why do you place `verifyElementPresent()` after `if () ... else ...` ?

 ![スクリーンショット 2022-02-27 7.54.56](https://europe1.discourse-cdn.com/katalon/original/3X/9/9/992fd7a408bb5bb38ac042036eff831394ed16c9.png)

In your case, you would want to place `verifyElementPresent()` **before** `if () ... else ...`.

 ![スクリーンショット 2022-02-27 7.54.56](https://europe1.discourse-cdn.com/katalon/original/3X/5/4/549dd6d66e768ba2d5341ec7ca94801c9e8a7b49.png)

* * *

> [@1Medy\_Albion](#):
>
> Yes I did researches. I used the function WebUI.verifyElementPresent.

I suppose that you misunderstood what `WebUI.verifyEementPresent` does + how to use it yet.

---

<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 27, 2022, 2:02am UTC](https://forum.katalon.com/t/how-to-find-the-total-number-of-rows-in-a-nested-table-using-xpath/62783/59 "2022-02-27T02:02:33Z")

</div>

![スクリーンショット 2022-02-27 11.00.56](https://europe1.discourse-cdn.com/katalon/original/3X/1/a/1ab2d309b23ade32de69eedf675850c912c26f99.png)

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