Unable to find element (table)

So i am trying to iterate through a table and click on some buttons.
For this I try something as simple as this:
WebDriver driverM = DriverFactory.getWebDriver()

WebElement TableM = driverM.findElement(By.id('W0241GridinfoContainerDiv'))

List<WebElement> RowsM = TableM.findElements(By.tagName('tr'))

println('No. of rows: ' + RowsM.size())

table: for (int i = 0; i < RowsM.size(); i++) {
    List<WebElement> ColsM = Rows.get(i).findElements(By.tagName('td'))

    for (int j = 0; j < ColsM.size(); j++) {
        println(ColsM.get(i).getText())

        ColsM.get(0).findElement(By.tagName('img')).click()
    }
}

The problem with this is that it does not find the table when the table is actually there.
I tried waiting for the table to appear and scroll to that table but it does not work. I also tried changing the id and instead of using the id of the table I use the id of the container but does not work. I tried to find the table by its xpath but it does not work either. I am running out of ideas or possible solutions. Do you have any suggestions?

This is the html of the table:

This is the error it throws:
org.openqa.selenium.NoSuchElementException: Unable to locate element: #W0241GridinfoContainerDiv

Hi @guillenoble59

From element with ID W0241GridinfoContainerDiv if you trace upward in the HTML structure do you see any iframe ? If the ID is correct then most likely the element is inside an iframe.

Because this…

findElement(By.id('W0241GridinfoContainerDiv'))

is searching for the div-element that hosts the table. It looks like you want

findElement(By.id('W0241GridinfoContainerTbl'))

Thanks for the response. I already tried doing that, but it didnt work.

Thanks for the response. I will try and see if that is happening.

Ok ok, so before iterating through the table I added a delay and this happens:

org.openqa.selenium.StaleElementReferenceException: The element reference of

is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
For documentation on this error

So I already know that the content is on the same frame and I obviously know that the content is there so the document must have been refreshed but why though? and how can I resolve this.

Type this in the browser DevTools console:

document.querySelector("#W0241GridinfoContainerTbl")

Take a screenshot of the result and post it here.

Thanks again for the response. This is what ir throws whenever I do that in the console

I already managed myself to access the first table, touch in the button marked in the image, and open the new table but for some reason I cannot access the second one.

Cool :sunglasses: We just proved your table is NOT in an iframe.

It sounds like the table (or the second one) is populated via XHR. You must find a way to trigger the change before you try to iterate over the table. That will stop the stale element error.

Thank you so much. I have no clue on how to do that, but I will do my best to try and solve it.

When you operate the site by hand, is it clear that the table appears after some human action (like click on a button or link, for example)?

If you identify that action, you need to repeat it in your test case (you may already be doing that). THEN you need to wait for the table to appear AND be populated with data.

You will very likely be doing a series of waitForElementVisible calls.

I am sorry but I already tried that. Didnt work !

I am sorry too. It WILL work.

To activate the table I click on the button mentioned before. Then I wait for the table to appear. Do I need to do something else?

Show me. Let me see that code.

Thanks for all the help, I managed myself to solve my problem using another method (different approach)
I will still post my code just because I am intrigued to know the solution for this problem:

WebUI.callTestCase(findTestCase('Esp/0-InicioSesion'), [:], FailureHandling.STOP_ON_FAILURE)

CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(findTestObject('Localhost Repository/Page_Inicio/a_Pre Analtica'), 
    30)

CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(findTestObject('Localhost Repository/Page_Inicio/a_Toma de Muestras'), 
    30)

CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(findTestObject('Localhost Repository/Page_Toma de Muestra/button_En Proceso'), 
    30)

CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(findTestObject('Localhost Repository/Page_Toma de Muestra/a_Seleccionar Todos'), 
    30)

WebUI.delay(3)

CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(findTestObject('Localhost Repository/Page_Toma de Muestra/input_Matricula_BUTTON1'), 
    30)

WebUI.delay(3)

String ExpectedValue = 'CI: 48031489'

WebDriver driver = DriverFactory.getWebDriver()

WebElement Table = driver.findElement(By.id('GridContainerDiv'))

List<WebElement> Rows = Table.findElements(By.tagName('tr'))

println('No. of rows: ' + Rows.size())

for (int i = Rows.size() - 1; i < Rows.size(); i++) {
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))

    //println(Rows.get(i).getText())
    for (int j = 0; j < Cols.size(); j++) {
        println(Cols.get(j).getText())

        if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
            WebUI.delay(3)

            //WebUI.scrollToPosition(Cols.get(0).findElement(By.tagName('img')).getLocation().getX(), Cols.get(0).findElement(By.tagName('img')).getLocation().getY())
            TestObject toV = new TestObject()

            String objectXpathV = CustomKeywords.'keywordPrueba.CustomFunction.getDynamicObjects'('//img[@id=\'vVER_', i)

            println(objectXpathV)

            toV.addProperty('xpath', ConditionType.EQUALS, objectXpathV)

            WebUI.waitForElementPresent(toV, 5)

            WebUI.scrollToElement(toV, 5)

            WebUI.delay(3)

            CustomKeywords.'keywordPrueba.CustomFunction.clickUsingJS'(toV, 30)

            //Cols.get(0).findElement(By.tagName('img')).click()
            break
        }
    }
}

WebUI.delay(3)

WebUI.waitForElementVisible(findTestObject('Localhost Repository/Page_Toma de Muestra/div_muestrasTable'), 30)

WebDriver driverM = DriverFactory.getWebDriver()

WebElement TableM = driverM.findElement(By.id('W0241GridinfoContainerDiv'))

List<WebElement> RowsM = TableM.findElements(By.tagName('tr'))

println('No. of rows: ' + RowsM.size())

table: for (int i = 0; i < RowsM.size(); i++) {
    List<WebElement> ColsM = Rows.get(i).findElements(By.tagName('td'))

    for (int j = 0; j < ColsM.size(); j++) {
        println(ColsM.get(i).getText())

        ColsM.get(0).findElement(By.tagName('img')).click()
    }
}

A post was split to a new topic: Can’t find table element