Case to reproducing StaleElementReferenceException #277

Closed
admizh opened this Issue Jan 30, 2016 · 7 comments

Projects

None yet

3 participants

@admizh
Contributor
admizh commented Jan 30, 2016

This test is demonstrate 100% reproduce throwing StaleElementReferenceException. How do we solve it?
Browser chrome: v 48.0.2564.97 m
Chrome driver 2.20

@Test()
    public void refException() {
        open("http://ya.ru");
        List<SelenideElement> elements = new ArrayList<>();
        for (SelenideElement selenideElement : $$("#text")) {
            elements.add(selenideElement);
        }
        executeJavaScript("window.location.reload();");
        elements.get(0).sendKeys("Val");
    }
java.lang.reflect.UndeclaredThrowableException
    at com.sun.proxy.$Proxy15.sendKeys(Unknown Source)
    at onboarding.demo.Demo4WorkWithCollection.refException(Demo4WorkWithCollection.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.codeborne.selenide.impl.SelenideElementProxy.dispatchAndRetry(SelenideElementProxy.java:82)
    at com.codeborne.selenide.impl.SelenideElementProxy.invoke(SelenideElementProxy.java:56)
    ... 25 more
Caused by: org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=48.0.2564.82)
  (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 34 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.49.1', revision: '808c23b0963853d375cbe54b90bbd052e2528a54', time: '2016-01-21 09:37:52'
System info: host: 'IJKN60267191A', ip: '192.168.1.230', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_72'
Driver info: framework.driver.WebDriverController$LoggableChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users\I301235\AppData\Local\Temp\scoped_dir7300_21717}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=48.0.2564.82, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 96f0e5a8d6601daecc57ed7f4d16fba0
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
@vinogradoff
Collaborator

Another version of semantically the same loop worked for me.
It's okay as workaround, but weird generally, we should look into it.

    //for (SelenideElement selenideElement : $$("#text")) {
    //  elements.add(selenideElement);
    //}
    ElementsCollection coll = $$("#text");
    for (int i = 0; i < coll.size(); i++) {
      elements.add(coll.get(i));
    }
@admizh
Contributor
admizh commented Jan 30, 2016

@vinogradoff sure. but sometimes we need collect some elements to list for perform other action. This example only presented for get exception. We should find way to solving it

@vinogradoff
Collaborator

This example collects elements to the list the same way as yours (but strangely with different result).

Could you try to make StaleElementException example without using „for (element: collection)“ ?

Am 30.01.2016 um 19:53 schrieb admizh notifications@github.com:

@vinogradoff https://github.com/vinogradoff sure. but sometimes we need collect some elements to list for perform other action. This example only presented for get exception. We should find way to solving it


Reply to this email directly or view it on GitHub #277 (comment).

@admizh
Contributor
admizh commented Jan 30, 2016

It is related with proxy, if use this: List elements = $$("#not_exist_locator");
problem should be solved, but method for add elemen to ElementsCollection not implemented.

  1. Need create staic method for get empty elementscollection.
  2. Method for add elements should be implemented (from AbstractList)
    @asolntsev you agree?
@asolntsev
Member

I don't understand why do you write such strange test. Why do you collect
elements to list? It's too complex. Test should be simple.
On Jan 30, 2016 7:35 PM, "admizh" notifications@github.com wrote:

This test is demonstrate 100% reproduce throwing
StaleElementReferenceException How do we solve it?
Browser chrome: v 480256497 m
Chrome driver 220

@Test()
public void refException() {
open("http://yaru");
List elements = new ArrayList<>();
for (SelenideElement selenideElement : $$("#text")) {
elementsadd(selenideElement);
}
executeJavaScript("windowlocationreload();");
elementsget(0)sendKeys("Val");
}

javalangreflectUndeclaredThrowableException
at comsunproxy$Proxy15sendKeys(Unknown Source)
at onboardingdemoDemo4WorkWithCollectionrefException(Demo4WorkWithCollectionjava:55)
at sunreflectNativeMethodAccessorImplinvoke0(Native Method)
at sunreflectNativeMethodAccessorImplinvoke(NativeMethodAccessorImpljava:62)
at sunreflectDelegatingMethodAccessorImplinvoke(DelegatingMethodAccessorImpljava:43)
at javalangreflectMethodinvoke(Methodjava:498)
at orgtestnginternalMethodInvocationHelperinvokeMethod(MethodInvocationHelperjava:86)
at orgtestnginternalInvokerinvokeMethod(Invokerjava:643)
at orgtestnginternalInvokerinvokeTestMethod(Invokerjava:820)
at orgtestnginternalInvokerinvokeTestMethods(Invokerjava:1128)
at orgtestnginternalTestMethodWorkerinvokeTestMethods(TestMethodWorkerjava:129)
at orgtestnginternalTestMethodWorkerrun(TestMethodWorkerjava:112)
at orgtestngTestRunnerprivateRun(TestRunnerjava:782)
at orgtestngTestRunnerrun(TestRunnerjava:632)
at orgtestngSuiteRunnerrunTest(SuiteRunnerjava:366)
at orgtestngSuiteRunnerrunSequentially(SuiteRunnerjava:361)
at orgtestngSuiteRunnerprivateRun(SuiteRunnerjava:319)
at orgtestngSuiteRunnerrun(SuiteRunnerjava:268)
at orgtestngSuiteRunnerWorkerrunSuite(SuiteRunnerWorkerjava:52)
at orgtestngSuiteRunnerWorkerrun(SuiteRunnerWorkerjava:86)
at orgtestngTestNGrunSuitesSequentially(TestNGjava:1244)
at orgtestngTestNGrunSuitesLocally(TestNGjava:1169)
at orgtestngTestNGrun(TestNGjava:1064)
at orgtestngIDEARemoteTestNGrun(IDEARemoteTestNGjava:72)
at orgtestngRemoteTestNGStartermain(RemoteTestNGStarterjava:122)
Caused by: javalangreflectInvocationTargetException
at sunreflectNativeMethodAccessorImplinvoke0(Native Method)
at sunreflectNativeMethodAccessorImplinvoke(NativeMethodAccessorImpljava:62)
at sunreflectDelegatingMethodAccessorImplinvoke(DelegatingMethodAccessorImpljava:43)
at javalangreflectMethodinvoke(Methodjava:498)
at comcodeborneselenideimplSelenideElementProxydispatchAndRetry(SelenideElementProxyjava:82)
at comcodeborneselenideimplSelenideElementProxyinvoke(SelenideElementProxyjava:56)
25 more
Caused by: orgopenqaseleniumStaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=480256482)
(Driver info: chromedriver=220353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 63 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 34 milliseconds
For documentation on this error, please visit: http://seleniumhqorg/exceptions/stale_element_referencehtml
Build info: version: '2491', revision: '808c23b0963853d375cbe54b90bbd052e2528a54', time: '2016-01-21 09:37:52'
System info: host: 'IJKN60267191A', ip: '1921681230', osname: 'Windows 81', osarch: 'amd64', osversion: '63', javaversion: '180_72'
Driver info: frameworkdriverWebDriverController$LoggableChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=C:\Users\I301235\AppData\Local\Temp\scoped_dir7300_21717}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=480256482, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 96f0e5a8d6601daecc57ed7f4d16fba0
at sunreflectNativeConstructorAccessorImplnewInstance0(Native Method)
at sunreflectNativeConstructorAccessorImplnewInstance(NativeConstructorAccessorImpljava:62)
at sunreflectDelegatingConstructorAccessorImplnewInstance(DelegatingConstructorAccessorImpljava:45)
at javalangreflectConstructornewInstance(Constructorjava:423)
at orgopenqaseleniumremoteErrorHandlercreateThrowable(ErrorHandlerjava:206)
at orgopenqaseleniumremoteErrorHandlerthrowIfResponseFailed(ErrorHandlerjava:158)
at orgopenqaseleniumremoteRemoteWebDriverexecute(RemoteWebDriverjava:678)


Reply to this email directly or view it on GitHub
#277.

@admizh
Contributor
admizh commented Jan 31, 2016

Ok, other examle.

    @Test
    public void second() {
        open("http://ya.ru");
        WebElement element = getWebDriver().findElement(By.cssSelector("#text"));
        SelenideElement wrapped = $(element);
        executeJavaScript("window.location.reload();");
        wrapped.sendKeys("Val");
    }

I think it difficult to solve, WebElementWrapper not provide re-search if element has changed...
This can be verified through the wrapper... https://github.com/barancev/webdriver-extensions/blob/master/webdriver-wrapper/src/main/java/ru/stqa/selenium/wrapper/StaleTolerantWrapper.java

@asolntsev asolntsev added a commit that closed this issue Jan 31, 2016
@asolntsev asolntsev Closes #277 $$ methods should re-read elements if that have been modi…
…fied/appeared/disappeared
87a3e99
@asolntsev asolntsev closed this in 87a3e99 Jan 31, 2016
@asolntsev
Member

@admizh @vinogradoff I have improved support for collections that are loaded slowly (with ajax).
So, the first test should become green with Selenide 3.3-SNAPSHOT

But it's not possible to make the last test green. The operator $(WebElement element) just wraps given WebElement instance. It cannot re-read or manipulate it in any way. This test is just bad. I still don't understand the real case for your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment