narsi
narsi

Reputation: 183

StaleElementReferenceException in Selenium WebDriver java test

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/[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 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..

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

Regards Narsi

Upvotes: 3

Views: 12572

Answers (2)

coldashes
coldashes

Reputation: 11

You can try this:

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

Upvotes: 0

Pavel Janicek
Pavel Janicek

Reputation: 14738

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.

What happens on the page when you manually click the colorButton? I expect that the page starts loading, or shows something.

You should probably add some waiting, or clicking somewhere else on the page

Upvotes: 5

Related Questions