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

Hello, I have tried many ways to get the number of rows in the table. But it just does not work. Please see my workings below.

Table

Test Case Code_1

WebDriver driver = DriverFactory. getWebDriver ()

WebElement Table = driver.findElement(By. xpath ("//*[@id='mlist']/tbody/tr"))

List<WebElement> rows_table = Table.findElements(By. tagName ('tr'))

int rows_count = rows_table.size()

println (rows_count)

Issue: It cannot find the element. But the xpath [(By. xpath (“//*[@id=‘mlist’]/tbody/tr”))] works well in dev mode.

Console Result

HTML Code

Test Case Code_2

WebDriver driver = DriverFactory.getWebDriver()
List<WebElement> rows_count=driver.findElements(By.xpath('//*[@id="mlist"]/tbody/tr'))
println rows_count.size()

Result: The number of rows is 0!

Console Result

How to find the total number of rows in the table? I will greatly appreciate if you can help me.

How about review the above?

1 Like

Hello, I executed it. The xpath is ok. All tags are under . It should work. But, each time it is giving the same error. Katalon cannot find the element: {“method”:“xpath”,“selector”:“id(‘mlist’)/tbody”}

Screenshots:


Can we use another function instead of By.xpath?

I guess your target HTML has a <iframe>, doesn’t it?

If you want to select a HTML element inside <iframe> you need to switch context into the document inside the <iframe> by

Hello @kazurayam. Yes, you are right! The target HTML is inside an iframe.

I have use the code:

WebUI.switchToFrame(findTestObject('Object Repository/Reported By/iFrame_ReportedBy'), 5)

WebUI.click(findTestObject('Reported By/letterB'))

WebDriver driver = DriverFactory.getWebDriver ()
WebElement Table = driver.findElement(By.xpath("id('mlist')/tbody/tr"))
List<WebElement> rows_table = Table.findElements(By.tagName('tr'))
int rows_count = rows_table.size()
		
println ("found ${rows_count} rows")

It has switched to the frame. But still giving the same error.

image

Exceptions | Selenium” says that a possible cause is the incorrect xPath in terms of invalid characters.

Questions: How can it be possible that the xpath may be incorrect when it is identifying each row in the table? Is it because the table is in an iframe that the xpath needs a modification? If so, then what is the modification?

I notice you are still using your old code and not the one I suggested. That’s why it’s not working.

Hello
I got the same error result when i used your code.

WebDriver driver = DriverFactory.getWebDriver () 

WebElement Table = driver.findElement(By.xpath("id('mlist')/tbody")) 
List<WebElement> rows_table = Table.findElements(By.tagName('tr')) 
int rows_count = rows_table.size()

println ("found ${rows_count} rows")

How do you detect the above quoted element? Like:
xpath = id("PopDiv")

And since the WebUI.switchToFrame() returns a boolean value, maybe you can use it to see if you have actually “switched”.

if (WebUI.switchToFrame(findTestObject('Object Repository/Reported By/iFrame_ReportedBy'), 5)) {

    WebUI.click(findTestObject('Reported By/letterB'))

    WebDriver driver = DriverFactory.getWebDriver ()
    WebElement Table = driver.findElement(By.xpath("id('mlist')/tbody"))
    List<WebElement> rows_table = Table.findElements(By.tagName('tr'))
    int rows_count = rows_table.size()
		
    println ("found ${rows_count} rows")
}

Once the iframe has opened, I inspect and detect it by using the xpath “//*[@id=“PopDiv”]”.


I ran your code.

if (WebUI.switchToFrame(findTestObject('Object Repository/Reported By/iFrame_ReportedBy'), 5)) {
	
		WebUI.click(findTestObject('Reported By/letterB'))
	
		WebDriver driver = DriverFactory.getWebDriver ()
		WebElement Table = driver.findElement(By.xpath("id('mlist')/tbody"))
		List<WebElement> rows_table = Table.findElements(By.tagName('tr'))
		int rows_count = rows_table.size()
			
		println ("found ${rows_count} rows")
	}

It is failing.

How do you modify the code to see it returns true?

WebUI.switchToFrame(findTestObject('Object Repository/Reported By/iFrame_ReportedBy'), 5, true)

Like this?

No. Within the if statement, the condition (the phrase within the parenthesis) has to be equal to a boolean. So, if the switchToFrame() would be true, then our table code would be run. If it would be false, the table code does not run.

If you want to print out the value, then you have to do something different. Maybe like (I added the FailureHandling part to the switchToFrame() statement so you need to add the FailureHandling import to the top):

if (WebUI.switchToFrame(findTestObject('Object Repository/Reported By/iFrame_ReportedBy'), 5, FailureHandling.OPTIONAL)) {
	println (" switchToFrame was true ")
	WebUI.click(findTestObject('Reported By/letterB'))
	
	WebDriver driver = DriverFactory.getWebDriver ()
	WebElement Table = driver.findElement(By.xpath("id('mlist')/tbody"))
	List<WebElement> rows_table = Table.findElements(By.tagName('tr'))
	int rows_count = rows_table.size()
			
	println ("found ${rows_count} rows")
} else {
	println (" switchToFrame was false")
}

You can structure it in many ways, but this would tell you what you want. Problem is to get the statement to run…

Thing is after clicking on the search icon, the iframe opens.

I have found this post:

I am thinking if “Parent iframe” must be used or not because it confuses things.

@grylion54 Thanks a lot! I executed it. It switched and clicked on B.

Now the problem is with the xpath(“id(‘mlist’)/tbody”).
Do you think before entering in the table, it should switch to DefaultContent and then again to the iframe?

@1Medy_Albion

You have your target URL in your hand, but others don’t.

Please understand that it is very difficult (impossible, I would say) for others on the forum to debug your Test Object and script without having the target HTML in hand.

c/c @sara.leslie


If possible, please save your target web page into a single MHTML file using Chrome DevTool, and share the file here.

In Chrome browser having your target HTML opened, if you right click on a page, in a drop down menu you can choose “Save as” which calls up a dialog where you can select to “save as, Webpage, Single file”. Do it and you will get a file. That’s a Mime HTML file.

If you could share the MHTML file, people on the forum will be able to reproduce your target HTML live on our PC without immediate internet connection to its URL. We will be able to hack for some solution.

If you could not share it, sorry I would not be able to help you any more.

@kazurayam Hello. I appreciate your help and that I am learning a lot!
I have the target URL. Voluntarily I would share the Mime HTML file if it was my property. But, it’s unlawful. What I can do is put all the screenshots of the iframe’s HTML as they are in a word doc.
Will it be ok?

No thank you.

Please imagine how much of efforts it will require me to locally “reproduce” your problem based on the screenshots provided. It is just terrible.

Unless “good enough” information provided, I would not like to be involved.

c/c @sara.leslie @Russ_Thomas

Your job require you highly skilled. Unfortunately your job doesn’t mind how you can get skilled enough. Good luck!

I would tell you how you can provide the best information, which is lawful, for communication in the forum.

You should look at the target HTML source code very carefully. And then, you should create a “mimic of the target page” manually by your hand, and try to reproduce your current problem with the mimic.

You should write a new HTML file manually locally which “works” like the original target Web page. You can refer to the original page while trimming “lawfully sensitive” portions off. You should write a test script against the hand-made target HTML.

Once you could reproduce the problem with the mimic, you can share the mimic HTML, which is lawfully secure to share, to others, can’t you?

Once you could reproduce the problem with the mimic, I believe, you would find a solution for yourself. You would not need asking for help to us any more.

@kazurayam @grylion54 Again thanks for all your efforts to help me get it working. I do understand that I have to make things easy for you to be able to help me. Many things I didn’t know, i am now aware thanks to you. I have the MHTML file.

Events.zip (387.2 KB)

Thank you for the MHTML exposed.

But you wrote that “it is unlawful”.

I do not want to be sued by somebody.

It’s perfectly OK to be used now!