Table dynamic Search

Hi @sanjoy2016ewu, welcome to our Katalon Community. Could you check if this doc is helpful for you?

Best,
Vu Tran

Have a look at the following guidance, and provide enough information.

I think you might have to employ a bit of what @vu.tran has about dynamic Test Objects and perhaps a bit of the below:

As @kazurayam posted, the more information you give us, the better our response back to you and perhaps it will be more helpful.

1 Like

Xpath: //input[@id=`grdDebitTrn_DrChk_0’]
This the xpath for first checkbox.
when i search another number for which I need to click on second check box or the checkbox where the search value contain. But every time katalon select the same checkbox for every different search

I have attached a photo of the table
Please give me a solution for this.

Are you using the same xpath to get to any other check box? We would need to see the HTML of your table to give more information about how to reach any other check box specifically (like maybe //input[@id='grdDebitTrn_DrChk_1']) but know that unless you are using parameters, one xpath leads to one element.

Does //table[1]/tbody/tr[2]/td[1] lead anywhere?

You should not stop in saying “serial number which I don’t know”. You should understand your problem first, and explain to others what you need to solve. Otherwise nobody can help you.

You must be aware how the serial numbers are formatted. You must be able to tell others how to imitate that serial numbers using your test script.

You should start with reading the HTML source of your target HTML.
… Do you know how to? May be not.

Then you should stop using Katalon Studio. It does not help you for understanding your target HTML.

You should start with learning how to use the DevTools of Web browsers. See the following post.

Then you might be able to learn how to parameterize a Test Object. See

https://docs.katalon.com/docs/katalon-studio-enterprise/test-design/web-test-design/web-test-objects/parameterize-web-test-objects

1 Like

Here may be one way to search the table for a specific “SL No” and then check the checkbox of that row.

def driver = DriverFactory.getWebDriver()

def requestedRef = "your SL No here"

WebElement table = driver.findElement(By.xpath('id("grdDebitTrn")/tbody'));

List<WebElement> rowsOfTable = table.findElements(By.tagName('tr'));

int rowCnt = rowsOfTable.size();
"start at 1 as we have to bypass the first row as it's a header"
for (int cnt = 1; cnt < rowCnt; cnt++) {
    List<WebElement> columnCells = rowsOfTable.get(cnt).findElements(By.tabName('td'));

    cellText = columnCells.get(1).getText();

    if (cellText == requestRef) {

        columnCells.get(0).click();
        break;
    }
}
1 Like

Sir it’s not working.
I need to click a checkbox by row value … and the value will change every time

Just so you know you have two tables: one of Debits and one of Credits. If you have to search both for a Serial Number, then it will get a bit more complicated. Saying that, I see that I missed a level. So try to find a Debit serial number with the following code:

Debit only maybe like:
driver = DriverFactory.getWebDriver()

def requestedRef = "your SL No here"

WebElement table = driver.findElement(By.xpath('id("grdDebitTrn")/tbody'));

List<WebElement> rowsOfTable = table.findElements(By.tagName('tr'));

int rowCnt = rowsOfTable.size();

"start at 1 as we have to bypass the first row as it's a header"
for (int cnt = 1; cnt < rowCnt; cnt++) {
    List<WebElement> columnCells = rowsOfTable.get(cnt).findElements(By.tagName('td'));

    cellText = columnCells.get(1).findElement(By.tagName('span')).getText();

    if (cellText == requestRef) {

        columnCells.get(0).findElement(By.tagName('span')).click();
        break;
    }
}
Debit or Credit maybe like:

If you have to search either Debit or Credit for your serial number, then perhaps the below may work:

driver = DriverFactory.getWebDriver()

def requestedRef = "your SL No here"

WebElement table = driver.findElement(By.xpath('id("grdDebitTrn")/tbody'));

List<WebElement> rowsOfTable = table.findElements(By.tagName('tr'));

boolean found = false;
int rowCnt = rowsOfTable.size();

"start at 1 as we have to bypass the first row as it's a header"
Loop:
for (int cnt = 1; cnt < rowCnt; cnt++) {
    List<WebElement> columnCells = rowsOfTable.get(cnt).findElements(By.tagName('td'));

    cellText = columnCells.get(1).findElement(By.tagName('span')).getText();

    if (cellText == requestRef) {
        found = true;
        columnCells.get(0).findElement(By.tagName('span')).click();
        break Loop;
    }
}


if (!found) {

    table = driver.findElement(By.xpath('id("grdCreditTrn")/tbody'));

    rowsOfTable = table.findElements(By.tagName('tr'));

    rowCnt = rowsOfTable.size();

"start at 1 as we have to bypass the first row as it's a header"
Loop:
for (int cnt = 1; cnt < rowCnt; cnt++) {
        columnCells = rowsOfTable.get(cnt).findElements(By.tagName('td'));

        cellText = columnCells.get(1).findElement(By.tagName('span')).getText();

        if (cellText == requestRef) {

            columnCells.get(0).findElement(By.tagName('span')).click();
            break Loop;
        }
    }
}
1 Like

Sir
I am facing problem in this line
List rowsOfTable - table.findElements(By.tagName(‘tr’));

Katalon showing Ted mark on rowaOfTable

My bad. This line should be:

List<WebElement> rowsOfTable = table.findElements(By.tagName('tr'));

Also, when you put code into this forum, put three back ticks, like ``` , above and below your code. The back tick is found on the same key as the tilde, like ~

Dear Sir,
I am using this as follow:

WebUI.openBrowser(‘’)

WebUI.navigateToUrl(‘file:///C:/Users/hp/Desktop/GS.html’)
driver = DriverFactory.getWebDriver()

def requestedRef = “3193263”

WebElement table = driver.findElement(By.xpath(‘id(“grdDebitTrn”)/tbody’));

List rowsOfTable = table.findElements(By.tagName(‘tr’));

int rowCnt = rowsOfTable.size();

for (int cnt = 0; cnt < rowCnt; cnt++) {
List columnCells = rowsOfTable.get(cnt).findElements(By.tabName(‘td’));

cellText = columnCells.get(1).findElement(By.tabName('span')).getText();

if (cellText == requestRef) {

	columnCells.get(0).findElement(By.tabName('span')).click();
	break;
}

}

But katalon studio are unable to proceed this.

i am unable to find the error here. Please help me.

Sorry for late reply.

A few fixes are needed and I apologize for a copy/paste error on my part. The first one I suggest is to put a wait statement after you “navigateToUrl” to allow the browser to open to the correct site. This is just a good safety measure.

WebUI.openBrowser('')

// like it better when see all the items
WebUI.maximizeWindow()
WebUI.navigateToUrl('file:///C:/Users/hp/Desktop/GS.html')
WebUI.waitForPageLoad(10)

The second is my copy/paste error. I put the phrase, “By.tabName” accidently, when it should be “By.tagName”–a “g” instead of a “b”. So we need to correct that.

Lastly, I failed to notice that there was no “td” tag on your first row, as that is your header. I either should have put in a safety “if” condition to prevent that or, as I did below, start the count from 1.

Maybe like:

The below code works on my desktop, but obviously I had to change the pathway. I hope this helps you.

WebUI.openBrowser('')

// like it better when see all the items
WebUI.maximizeWindow()
WebUI.navigateToUrl('C://Users//Mike//Desktop//HTML FOR MY TABLE.html')
WebUI.waitForPageLoad(10)

driver = DriverFactory.getWebDriver()

def requestedRef = "3193263"

WebElement table = driver.findElement(By.xpath('id("grdDebitTrn")/tbody'));

List<WebElement> rowsOfTable = table.findElements(By.tagName('tr'));

int rowCnt = rowsOfTable.size();
println ("row count is ${rowCnt}")
WebUI.comment("row count is ${rowCnt}")
Loop:
for (int cnt = 1; cnt < rowCnt; cnt++) {

	List<WebElement> columnCells  = rowsOfTable.get(cnt).findElements(By.tagName('td'))
	
   'To calculate no of columns(cells) in that specific row'
   int column_count = columnCells.size()
	
//	cellText = columnCells.get(1).findElement(By.xpath("./child::span")).getText();
	cellText = columnCells.get(1).findElement(By.tagName('span')).getText();
	
	if (cellText == requestedRef) {
	
//		columnCells.get(0).findElement(By.xpath("./child::span")).click();
		columnCells.get(0).findElement(By.tagName('span')).click();
		break Loop;
	}
}

I also tried it another way:

Maybe like:
WebUI.openBrowser('')

// like it better when see all the items
WebUI.maximizeWindow()
WebUI.navigateToUrl('C://Users//Mike//Desktop//HTML FOR MY TABLE.html')
WebUI.waitForPageLoad(10)

driver = DriverFactory.getWebDriver()

"see if it can click on any other"
def requestedRef = "3313844"  //"3193263"

List<WebElement> rows = driver.findElements(By.xpath('//tr[contains(@class, "Dr_Row")]/td[contains(text(), "IMP BILL LODGEMENT")]'));

int rowCnt = rows.size();

for (int cnt = 0; cnt < rowCnt; cnt++) {
	
	String part_xpath = "grdDebitTrn_lblSlNo_${cnt}"
	columnSLNo = driver.findElement(By.xpath("id('${part_xpath}')"))
	
	cellText = columnSLNo.getText();
	
	if (cellText == requestedRef) {
		// columnSLNo.findElement(By.xpath("parent::td/preceding-sibling::td[1]/span")).click();
		checkBox = columnSLNo.findElement(By.xpath("parent::td/preceding-sibling::td[1]/span"))
		
		WebUI.scrollToPosition(100, checkBox.getLocation().getY())
		WebUI.delay(1)
		
		checkBox.click();
		break;
	}
}

Dear Sir,
I am doing it like this
WebUI.openBrowser(‘’)

WebUI.maximizeWindow()

WebUI.navigateToUrl(‘C:/Users/hp/Downloads/GS.html’)

WebUI.waitForPageLoad(10)

driver = DriverFactory.getWebDriver()

def requestedRef = ‘3193263’

WebElement table = driver.findElement(By.xpath(‘id(“grdDebitTrn”)/tbody’))

List rowsOfTable = table.findElements(By.tagName(‘tr’))

int rowCnt = rowsOfTable.size()

println(“row count is $rowCnt”)

WebUI.comment(“row count is $rowCnt”)

for (int cnt = 1; cnt < rowCnt; cnt++) {
List columnCells = rowsOfTable.get(cnt).findElements(By.tagName(‘td’))

'To calculate no of columns(cells) in that specific row'
int column_count = columnCells.size()

//	cellText = columnCells.get(1).findElement(By.xpath("./child::span")).getText();
cellText = columnCells.get(1).findElement(By.tagName('span')).getText()

if (cellText == requestedRef) {
    //		columnCells.get(0).findElement(By.xpath("./child::span")).click();
    columnCells.get(0).findElement(By.tagName('span')).click()

    break
}

}
But still its not working. You have said that you changed the pathway. But i am not understanding how to change the pathway.
please help me. and sorry for disturbing you again and again.

I want to search and click on the checkbox

Sir what can I do now… Please give me a way please …

Is any need to count the row s? I just need to search the serial number and want to check that checkbox

Just for your information, when you see code that is underlined by KS, then those variables have an issue; in this case, they are undefined. In other words, KS does not know what they are. In this case, they are undefined because you are missing the appropriate import statement. You can either hit CTRL + SHIFT + O (oh) to add the import statements automatically or add them manually, starting with:

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

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   //this is the one you're missing
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable