我在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)
答案 0 :(得分:1)
如果您有一个WebElement
,然后源代码发生变化,然后您尝试读取或写入元素,就会发生这种情况。如果对页面进行了更改,则不能再使用该元素。在页面更改后,您应该尝试返回元素。例如,保存元素id
,然后保存findElement(By.id(...))
。
答案 1 :(得分:1)
如果您的页面/元素通过刷新或更新卸载,安全的方法是再次找到您的元素。
如果您正在使用Page Object设计模式,我认为获取StaleElementException并不容易,因为它使用动态代理,并且每个命令都有查找。如果您使用的是PageObject,并且在该字段上有@CacheLookup注释,请将其删除并重试。
答案 2 :(得分:0)
根据您提供的信息,Selenium似乎未能找到应该存在的元素。你能否将这个错误精确定位到页面上的单个元素?
看起来selenium正在尝试安排点击事件而无法安排。测试期间显示屏上有什么变化?
答案 3 :(得分:0)