Selenium WebDriver java测试中的StaleElementReferenceException

时间:2012-04-12 05:43:28

标签: java selenium selenium-webdriver

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@googlecode.com/components/command_processor.js:10200)
    at .(file:///C:/DOCUME~1/npolic/LOCALS~1/Temp/anonymous3371533678733192941webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10219)
    at .(file:///C:/DOCUME~1/npolic/LOCALS~1/Temp/anonymous3371533678733192941webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10026)

Mycode就是这样写的。这是连续的行动。第一个动作是第一次在循环内工作正常。当第二个循环开始时,它抛出StaleElementReferenceException。所以请帮助我,如何逐个执行多个动作..

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

2 个答案:

答案 0 :(得分:4)

基本上StaleElementReferenceException发生在页面仍在加载或更改内容时 - 所以某些元素就在那里,但发生了一些事情,而且元素在DOM中不再存在。

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

您应该添加一些等待,或点击页面上的其他位置

答案 1 :(得分: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);
}