1

在我的 Android 模拟器上执行我的 selenium 测试用例时,我遇到了以下错误:

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)
4

4 回答 4

1

如果你有一个WebElement,就会发生这种情况,然后源代码发生变化,然后你尝试读取或写入元素。如果对页面进行了更改,则该元素将无法再使用。在页面更改后,您应该尝试取回元素。例如保存元素id,然后再做findElement(By.id(...)).

于 2013-11-05T11:34:26.210 回答
1

如果您的页面/元素通过刷新或更新卸载,安全的方法是再次找到您的元素。

如果您使用的是页面对象设计模式,我相信 StaleElementException 并不容易,因为它使用动态代理,每个命令都有一个查找。如果您使用的是 PageObject,并且在该字段上有 @CacheLookup 注释,请删除它并重试。

于 2013-11-05T20:05:41.640 回答
0

根据您提供的信息,Selenium 似乎无法找到应该存在的元素。您能否将此错误精确到页面上的单个元素?

看起来 selenium 正在尝试安排点击事件,但无法做到。测试期间您的显示器上有什么变化?

于 2013-11-05T11:26:23.513 回答
0

http://staleelementreferenceexception.blogspot.in/2014/09/orgopenqaseleniumstaleelementreferencee.html

上面的链接为这个问题提供了一个惊人的解决方案

于 2014-09-24T06:44:44.777 回答