2012-04-12 83 views
3
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$1.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$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.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/fxdriver[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) 

Mycode已经写成这样。这是为了连续行动。第一次行动在第一次循环内正常工作。当第二个循环开始时,它会抛出StaleElementReferenceException。所以,请大家帮帮我,怎么一个一个地执行多个操作..Selenium WebDriver中的StaleElementReferenceException java测试

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(); 
} 

问候 Narsi

回答

4

基本上StaleElementReferenceException发生在当页面仍在加载,或改变内容 - 所以一些元素在那里,但是发生了一些事情,而且这个元素不再在DOM中。

手动单击colorButton时页面上发生了什么?我期望页面开始加载,或者显示一些内容。

你或许应该增加一些等待,或点击其他地方的页面

0

上你可以试试这个:

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); 
} 
+0

这将会是更好的,如果你解释你的代码。另外,for循环没有关闭花括号。 – 2012-10-22 00:03:25

相关问题