Hello everyone,
I got the “error stale element : element is not attached to the page document” when handling a table.
This is my context :
Here is the table:
I want to search in each column of all rows which have 2018 and click Delete of this rows. When clicking the Delete link, a new page will appear and il’ll continue my test.
I use the following code :
String expectedContract = '2018'
WebDriver driver = DriverFactory.getWebDriver()
WebElement siteTable = driver.findElement(By.xpath('//table[@class="contractList widgetBodyTable"]'))
List<WebElement> rows = siteTable.findElements(By.tagName('tr'))
for (int i = 0; i < rows.size(); i++) {
List<WebElement> cels = rows.get(i).findElements(By.tagName('td'))
for (int j = 0; j < cels.size(); j++) {
if (cels.get(j).getText().equalsIgnoreCase(expectedContract)) {
try {
wait.until(ExpectedConditions.presenceOfElementLocated(cels))
cels.get(5).findElement(By.tagName('a')).click()
break
} catch(StaleElementReferenceException e) {
driver.navigate().refresh()
cels.get(5).findElement(By.tagName('a')).click()
}
}
}
}
When executing this code, It found the element that I want to click, It perform the click and pass to another page. But after that, It throws error “error stale element : element is not attached to the page document”. Here is the error in console:
Reason:
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=109.0.5414.75)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'GA4PFL7S3', ip: '10.21.80.45', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_282'
Driver info: com.kms.katalon.selenium.driver.CChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 109.0.5414.75, chrome: {chromedriverVersion: 109.0.5414.74 (e7c5703604da..., userDataDir: C:\Users\ttmooc\AppData\Loc...}, goog:chromeOptions: {debuggerAddress: localhost:57987}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 880b29b8431823b15c2440cf26587202
*** Element info: {Using=tag name, value=td}
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at com.kms.katalon.selenium.driver.CChromeDriver.execute(CChromeDriver.java:19)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:204)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:281)
at org.openqa.selenium.By$ByTagName.findElements(By.java:312)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:177)
at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.lambda$new$0(EventFiringWebDriver.java:404)
at com.sun.proxy.$Proxy11.findElements(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.findElements(EventFiringWebDriver.java:504)
at org.openqa.selenium.WebElement$findElements$0.call(Unknown Source)
at Handle Table.run(Handle Table:70)
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:448)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1673444668268.run(TempTestCase1673444668268.groovy:25)
Can some one help to fix this error please? Thank you