Error when handle Web Tables

Hello, i new in katalon studio, this is my skenario

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement

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

WebUI.openBrowser(’’)

String ExpectedValue = ‘31824174271065917763’

WebDriver driver = DriverFactory.getWebDriver()

WebElement Table = driver.findElement(By.xpath(’//[@id=“kt_content”]/div[2]/div/div[1]/div[2]/div[1]/table/tbody’))

List Rows = Table.findElements(By.tagName(‘tr’))

System.out.println(Rows.size())

table: for (int i = 0; i < Rows.size(); i++) {

println(Rows.get(i).getText())

List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))

for (int j = 0; j < Cols.size(); j++) {
	
	println (Cols.get(j).getText())
	
	if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
					
		 Cols.get(5).findElement(By.tagName('a')).click()

		break
	}
}

}

And then this is error

java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
at java_util_List$get$1.call(Unknown Source)
at Create Adjustment Reconcile.run(Create Adjustment Reconcile:50)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1600070359413.run(TempTestCase1600070359413.groovy:23)

this code can find my expected value (31824174271065917763) but can’t click button. Error in this code
Cols.get(5).findElement(By.tagName(‘a’)).click()

And this is my html code, i want to click

360001FE902542081916 36020200836000100 KREDIT ALREADY ADJUSTED **** Stockholm-icons / Communication / WriteCreated with Sketch.

Please help me, thank you

@nurprasetyo.kurniawa You do not need the findElement bit. Shorten the statement to:

Cols.get(5).click()

This assumes you want to click on the link from the sixth column. Is that correct? If you want the link from the fifth column, then you should use the index 4. The array is zero based. So zero is the first column, 1 would be the second, etc.

If the link is in the last position, then you might use the array index as: Cols.size() - 1

1 Like

Let’s take above HTML table as an example:

WebElement htmltable=driver.findElement(By.xpath("//*[@id=‘main’]/table[1]/tbody"));
List rows=htmltable.findElements(By.tagName(“tr”));

for(int rnum=0;rnum<rows.size();rnum++)
{
List columns=rows.get(rnum).findElements(By.tagName(“th”));
System.out.println(“Number of columns:”+columns.size());

for(int cnum=0;cnum<columns.size();cnum++)
{
System.out.println(columns.get(cnum).getText());
}
}
Step 1: First get the entire HTML table and store this in a variable ‘htmltable’ of type web element.

Step 2: Get all the rows with tag name ‘tr’ and store all the elements in a list of web elements. Now all the elements with tag ‘tr’ are stored in ‘rows’ list.

Step 3: Loop through each row and get the list of elements with tag ‘th’. ‘rows.get(0)’ will give first row and ‘findElements(By.tagName(“th”))’ will give list of columns for the row.

Step 4: Iterate using ‘columns.getsize()’ and get the details of each cell.

Note: Above approach will be best suitable if the table dimensions changes dynamically.

This concludes the topic how to handle web tables in selenium. Next, we will learn about handling an element inside a frame.

Thanks alot @grylion54
problem solved