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:
- String ExpectedValue = ‘CI: 47241111’ //get the value I want to find in the table
- WebElement Table = driver.findElement(By.id(‘TABLEMAIN’)) //get table by its ID
- List Rows = Table.findElements(By.tagName(‘tr’)) //get rows
- //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: