Hello community.
I am facing a problem for searching table data in web testing by katalon studio.
In my test I have to search a number in the table and then I have to click the checkbox. But every time I search a new number katalon studio can’t find the right checkbox for the searched number.
The checkbox contain different serial number which I don’t know…
In this case what I can do for the dynamic checkbox selection and click …
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
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.
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
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;
}
}
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;
}
}
}
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 ~
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;
}
}
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;
}
}
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.