Stale element exception in web table

Same Problem. Have you been able to figure out a solution?
Thanks!

I’m trying to read the following table on my webpage.

and I’m getting staleElementReference on Cols = get(i).FindElements(by.tagName(“td”)

This is what you do:

  1. You get all rows from a table
  2. You filter out some of them (using search “test”)
  3. You are trying to access all rows (not only currently visible ones)

My suggestion is to move your search routine outside of foreach loop (moreover, it makes no sense even without this issue), then get all rows and finally use foreach to get all cells within filtered rows.

Hi Marek,

My search routine was actually outside the for loop only. I just added it to try to avoid the stale error.
I’m still getting the same error at a different location.

Any other ideas? Thanks.

Can you share your script code and full exception?

I will move these posts to a separate thread, because it is not related to the original topic.

Here’s what my script looks like:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser('')
WebUI.navigateToUrl('https://vpcptwebtest.xsxw.com/PlayerNotification/Account/Login?ReturnUrl=%xsxw%2f')
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input_Username_Username'), 'ewerr')
WebUI.setEncryptedText(findTestObject('Object Repository/Page_XOXO/input_Password_Password'), 
    'tzH6RvlsweTg=')
WebUI.click(findTestObject('Object Repository/Page_XOXO/button_Login'))
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input__player'), 'TestFaizan TestPlayer')
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input__RecipientName'), 'Katalontest Recip')
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input_Phone Number_PhoneNumber'), '91423432426')
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input__EmailAddress'), 'katalon-test@gmo.net')
WebUI.click(findTestObject('Object Repository/Page_XOXO/button_Submit'))
WebUI.verifyElementPresent(findTestObject('Object Repository/Page_XOXO/div_Success Katalontest Recip has been added to the notification list for TestFaizan TestPlayer'), 0)
WebUI.click(findTestObject('Object Repository/Page_XOXO/a_Look Up'))
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input_Search_SearchTerm'), 'test')
WebUI.click(findTestObject('Object Repository/Page_XOXO/button_Search'))
ExpectedValue = 'Katalontest Recip'
WebDriver driver = DriverFactory.getWebDriver()
WebUI.click(findTestObject('Object Repository/Page_XOXO/a_Look Up'))
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input_Search_SearchTerm'), 'test')
WebUI.click(findTestObject('Object Repository/Page_XOXO/button_Search'))
WebElement Table = driver.findElement(By.xpath('//table/tbody'))
List<WebElement> Rows = Table.findElements(By.tagName('tr'))
println('No of Rows:' + Rows.size())
//List<WebElement> Rows = driver.findElements(By.xpath("//table//td[contains(text(),ExpectedValue)]"));
table: for (int i = 0; i < Rows.size(); i++) {
    'To locate columns(cells) of that specific row'
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
    for (int j = 0; j < Cols.size(); j++) {
        'Verifying the expected text in the each cell'
        if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {
            'To locate anchor in the expected value matched row to perform action'
            Cols.get(5).findElement(By.tagName('a')).click()
            break
        }
    }
}
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input__Email'), 'katalon-test@@gmo.net')
WebUI.click(findTestObject('Object Repository/Page_XOXO/button_Save Changes'))
WebUI.verifyTextPresent('Incorrect email', false)
WebUI.setText(findTestObject('Object Repository/Page_XOXO/input__Email'), 'katalon-testGood@gmo.net')
WebUI.click(findTestObject('Object Repository/Page_XOXO/button_Save Changes'))
WebUI.verifyTextPresent('katalon-testGood@gmo.net', false)
WebUI.click(findTestObject('Object Repository/Page_XOXO/a_pservices1'))
WebUI.click(findTestObject('Object Repository/Page_XOXO/a_Logout'))
WebUI.closeBrowser()

Here’s the exception:

2019-04-24 13:47:48.766 ERROR c.k.katalon.core.main.TestCaseExecutor - ❌ for ([i = 0, i &lt; Rows.size(), (i++)]) FAILED.

Reason:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

(Session info: chrome=73.0.3683.103)

(Driver info: chromedriver=2.43.600229 (3fae4d0cda5334b4f533bede5a4787f7b832d052),platform=Mac OS X 10.14.4 x86_64) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 0 milliseconds

For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html

Build info: version: '3.7.1', revision: '8a0099a', time: '2017-11-06T21:07:36.161Z'

System info: host: 'localhost', ip: 'fe80:0:0:0:142a:876:1aeb:cea8%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.4', java.version: '1.8.0_181'

Driver info: com.kms.katalon.selenium.driver.CChromeDriver

Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.43.600229 (3fae4d0cda5334..., userDataDir: /var/folders/hr/dgj_n2x9269...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:62638}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 73.0.3683.103, webStorageEnabled: true}

Session ID: 6131a7f6c72523b60a4abe53d326ad96

at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)

at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)

at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)

at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)

at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)

at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)

at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)

at com.kms.katalon.selenium.driver.CChromeDriver.execute(CChromeDriver.java:20)

at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)

at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:160)

at org.openqa.selenium.WebElement$getText$0.call(Unknown Source)

at Add Recipient - PN.run(Add Recipient - PN:61)

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:331)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:322)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:301)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:293)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:227)

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 TempTestCase1556128060349.run(TempTestCase1556128060349.groovy:21)

2019-04-24 13:47:48.769 ERROR c.k.katalon.core.main.TestCaseExecutor - ❌ Test Cases/Add Recipient - PN FAILED.

Reason:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

(Session info: chrome=73.0.3683.103)

(Driver info: chromedriver=2.43.600229 (3fae4d0cda5334b4f533bede5a4787f7b832d052),platform=Mac OS X 10.14.4 x86_64) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 0 milliseconds

For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html

Build info: version: '3.7.1', revision: '8a0099a', time: '2017-11-06T21:07:36.161Z'

System info: host: 'localhost', ip: 'fe80:0:0:0:142a:876:1aeb:cea8%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.4', java.version: '1.8.0_181'

Driver info: com.kms.katalon.selenium.driver.CChromeDriver

Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.43.600229 (3fae4d0cda5334..., userDataDir: /var/folders/hr/dgj_n2x9269...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:62638}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 73.0.3683.103, webStorageEnabled: true}

Session ID: 6131a7f6c72523b60a4abe53d326ad96

at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)

at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)

at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)

at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)

at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)

at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)

at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)

at com.kms.katalon.selenium.driver.CChromeDriver.execute(CChromeDriver.java:20)

at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)

at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:160)

at org.openqa.selenium.WebElement$getText$0.call(Unknown Source)

at Add Recipient - PN.run(Add Recipient - PN:61)

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:331)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:322)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:301)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:293)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:227)

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 TempTestCase1556128060349.run(TempTestCase1556128060349.groovy:21)

2019-04-24 13:47:48.772 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/Add Recipient - PN