简体   繁体   中英

org.openqa.selenium.StaleElementReferenceException: WebElement is stale

I am facing the below error while executing my selenium test case on my Android Emulator:

org.openqa.selenium.StaleElementReferenceException: WebElement is stale.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: os.name: 'Linux', os.arch: 'i686', os.version: '3.4.0+', java.version: '0'
Driver info: driver.version: AndroidWebDriver
Command duration or timeout: 13.02 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:14:00'
System info: host: 'HW0645', ip: '172.16.6.189', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_20'
Session ID: 2133bbec-d332-42ed-bc25-959af8c7eb03
Driver info: org.openqa.selenium.android.AndroidDriver
Capabilities [{platform=ANDROID, javascriptEnabled=true, acceptSslCerts=true, browserName=android, rotatable=true, locationContextEnabled=true, webdriver.remote.sessionid=2133bbec-d332-42ed-bc25-959af8c7eb03, version=18, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)
    at pack.HallMarkApp_Demo.testGoogle(HallMarkApp_Demo.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.openqa.selenium.StaleElementReferenceException: WebElement is stale.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: os.name: 'Linux', os.arch: 'i686', os.version: '3.4.0+', java.version: '0'
Driver info: driver.version: AndroidWebDriver
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:14:00'
System info: host: 'HW0645', ip: '172.16.6.189', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_20'
Driver info: driver.version: AndroidWebDriver
    at org.openqa.selenium.android.library.AndroidWebDriver.throwIfError(AndroidWebDriver.java:1015)
    at org.openqa.selenium.android.library.AndroidWebDriver.executeRawScript(AndroidWebDriver.java:895)
    at org.openqa.selenium.android.library.AndroidWebDriver.injectJavascript(AndroidWebDriver.java:930)
    at org.openqa.selenium.android.library.AndroidWebDriver.executeScript(AndroidWebDriver.java:786)
    at org.openqa.selenium.android.library.AndroidWebElement.getTagName(AndroidWebElement.java:203)
    at org.openqa.selenium.android.library.AndroidWebElement.click(AndroidWebElement.java:125)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement$1.invoke(EventFiringWebDriver.java:327)
    at $Proxy2.click(Native Method)
    at org.openqa.selenium.support.events.EventFiringWebDriver$EventFiringWebElement.click(EventFiringWebDriver.java:340)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at org.openqa.selenium.remote.server.KnownElements$1.invoke(KnownElements.java:63)
    at $Proxy3.click(Native Method)
    at org.openqa.selenium.remote.server.handler.ClickElement.call(ClickElement.java:29)
    at org.openqa.selenium.remote.server.handler.ClickElement.call(ClickElement.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:169)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:841)

This happens if you have got a WebElement , then the source code changes and then you try to read or write something to the element. The element just can't be used anymore if changes are made to the page. You should try to get the element back, after the page changed. For example save the elements id and later do findElement(By.id(...)) .

Safe way is to find your element again if your page/elements got unloaded via a refresh or update.

If you are using the Page Object design pattern, I believe it is not easy to get StaleElementException as it uses dynamic proxies, with a lookup per command. If you are using a PageObject, and have @CacheLookup annotation on the field, please remove that and try again.

From the information you have given, it appears that Selenium is failing to find an element that should be there. Can you pinpoint this error down to a single element on the page?

It looks like selenium is trying to schedule a click event and is unable to. What is changing on your display during the test?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM