Selenium WebDriver java 测试中的 StaleElementReferenceException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10118013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StaleElementReferenceException in Selenium WebDriver java test
提问by narsi
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up Command duration or timeout: 30.02 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.18.0', revision: '15704', time: '2012-01-27 17:37:17' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_17' Driver info: driver.version: RemoteWebDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:147) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:113) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:439) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:231) at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:72) at com.harleydavidson.modelyear.selenium2.testcase.ImageTest.checkColor(ImageTest.java:79) at com.harleydavidson.modelyear.selenium2.testcase.ImageTest.chooseModel(ImageTest.java:62) at com.harleydavidson.modelyear.selenium2.testcase.ImageTest.testImage(ImageTest.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner.run(ParentRunner.java:231) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access0(ParentRunner.java:50) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 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.remote.ErrorHandler$UnknownServerException: Element not found in the cache - perhaps the page has changed since it was looked up Build info: version: '2.18.0', revision: '15704', time: '2012-01-27 17:37:17' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_17' Driver info: driver.version: unknown at .(resource://fxdriver/modules/atoms.js:9557) at .(resource://fxdriver/modules/atoms.js:10153) at .(file:///C:/DOCUME~1/npolic/LOCALS~1/Temp/anonymous3371533678733192941webdriver-profile/extensions/[email protected]/components/command_processor.js:10200) at .(file:///C:/DOCUME~1/npolic/LOCALS~1/Temp/anonymous3371533678733192941webdriver-profile/extensions/[email protected]/components/command_processor.js:10219) at .(file:///C:/DOCUME~1/npolic/LOCALS~1/Temp/anonymous3371533678733192941webdriver-profile/extensions/[email protected]/components/command_processor.js:10026)List<WebElement> colorButtons = driver.findElements(By.xpath("/html/body/div[4]/div[2]/div/div[4]/div[2]/div/img")); for(WebElement colorButton : colorButtons) { colorButton.click(); }
Mycode has been written like this. It is for consecutive actions. First action is working fine inside loop for first time. when second loop started, it is throwing StaleElementReferenceException . So please help me , how to perform multiple actions one by one..
Mycode 就是这样写的。它用于连续动作。第一个动作是第一次在循环内正常工作。当第二个循环开始时,它抛出 StaleElementReferenceException 。所以请帮助我,如何一一执行多个操作..
List<WebElement> elements = driver.findElements(By. xpath abc));
for(int i=0; i<elements.size(); i++){
WebElement ele = null;
if(i<3){
List<WebElement> elements2 = driver.findElements(By. xpath abc);
ele = elements2.get(i);
}
Regards Narsi
问候纳尔西
回答by Pavel Janicek
Basically the StaleElementReferenceException
happens when the page is still loading, or changing the content - so some element was there, but something happened and that element ios no more in the DOM.
基本上,StaleElementReferenceException
当页面仍在加载或更改内容时会发生这种情况 - 所以有些元素在那里,但发生了一些事情并且该元素 ios 不再存在于 DOM 中。
What happens on the page when you manually click the colorButton? I expect that the page starts loading, or shows something.
当您手动单击 colorButton 时,页面上会发生什么?我希望页面开始加载,或者显示一些东西。
You should probably add some waiting, or clicking somewhere else on the page
您可能应该添加一些等待,或单击页面上的其他位置
回答by coldashes
You can try this:
你可以试试这个:
##代码##