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?
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.
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.
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 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.
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.
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()
}
}