Table iteration is not working correctly

Hi again,
What I am trying to do is to iterate through all the elements of a table. If I find a certain value in one of these elements, then I click on a button this element has. I do this by doing the next thing:

  1. String ExpectedValue = ‘CI: 47241111’ //get the value I want to find in the table
  2. WebElement Table = driver.findElement(By.id(‘TABLEMAIN’)) //get table by its ID
  3. List Rows = Table.findElements(By.tagName(‘tr’)) //get rows
  4. //start loop
    table: for (int i = 0; i < Rows.size(); i++) {
    println(Rows.get(i).getText())
    List Cols = Rows.get(i).findElements(By.tagName(‘td’)) //get columns
    for (int j = 0; j < Cols.size(); j++) {
    if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
    Cols.get(0).findElement(By.tagName(‘img’)).click() //click button
    break
    }
    }
    }

I know this code works correctly because I used it on a different table located on a different page but in this case the test does not pass because it is not finding the expected value correctly.
Sometimes it throws an error like this:
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java_util_List$get$1.call(Unknown Source)
at IngresoYValidacionResultados.run(IngresoYValidacionResultados:77)
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 TempTestCase1561726041485.run(TempTestCase1561726041485.groovy:21)

and sometimes it throws a stale element reference exception.

I use a println like this Rows.get(i).getText() to see what elements is looking at and it throws elements from a different table in the same page. It is weird because this two tables have different id and the one that I am using is the one that I want. Am I missing something? Am i doing something wrong? I need help cause I am trying to solve this but nothing seems to work.

This is the html surrounding the table:

OK ! I used a different ID. I used the id the container of the table has and the test case passes correctly. The thing is that it is not loading the page it should load when I click in the button. I will do a little bit of more research on this and see why it is not working but any suggestion is welcomed.

mmm… it is not entering the second for of the entire loop and to be honest the firs print is printing nothing when it should not because there is stuff on the table
table: for (int i = 0; i < Rows.size(); i++) {
println("Row " + Rows.get(i).getText())
List Cols = Rows.get(i).findElements(By.tagName(‘td’))
for (int j = 0; j < Cols.size(); j++) {
println("Cols " + Cols.get(j).getText())
if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
Cols.get(0).findElement(By.tagName(‘img’)).click()
break
}
}
}

Sooo what can I do to solve this?