Single Post

Header

Monday, November 23, 2015

org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

When do we get "StaleElementReferenceException" while doing with Selenium Web Driver

package SeleniumPractise;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;

public class StaleElementReferenceException {

  @Test
  public void getStaleElementReferenceException() throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://docs.seleniumhq.org/");
    driver.manage().window().maximize();
    Thread.sleep(2000);
    List<WebElement> links = driver.findElements(By.tagName("a"));
    for(WebElement link : links) {
      System.out.println(link.getText());
    }
 
    for(WebElement link : links) {
  // We get error here, after click on link and come back to the link web element which is not available
 //  Because the page is moved to the other page where the link web element is not there
      if(link.getText().equals("Who made Selenium"))   {
        link.click();
        Thread.sleep(2000);
      }
    }
 
    driver.close();
  }

 @Test
  public void fixStaleElementReferenceException() throws InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://docs.seleniumhq.org/");
    driver.manage().window().maximize();
    Thread.sleep(2000);
    List<WebElement> links = driver.findElements(By.tagName("a"));
    for(WebElement link : links) {
      System.out.println(link.getText());
    }
 
    for(WebElement link : links) {
      if(link.getText().equals("Who made Selenium"))   {
        link.click();
        Thread.sleep(2000);
        break;
      }
    }
 
    driver.close();
  }


}


Error Logs for the first menthod above :

org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 18 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'vindukuri1.blr.corp.<company>.com', ip: '172.30.104.73', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-71-generic', java.version: '1.8.0-google-v7'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=43.0, platform=LINUX, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b2af66d3-35e0-47f0-8e85-c3d05417812b
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:326)
at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:178)
at SeleniumPractise.getLinksOnPage.getAllLinksOnAWebPage(getLinksOnPage.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'vindukuri1.blr.corp.google.com', ip: '172.30.104.73', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-71-generic', java.version: '1.8.0-google-v7'
Driver info: driver.version: unknown
at <anonymous class>.fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:9351)
at <anonymous class>.Utils.getElementAt(file:///tmp/anonymous5472246950201116367webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:8978)
at <anonymous class>.WebElement.getElementText(file:///tmp/anonymous5472246950201116367webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:11965)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///tmp/anonymous5472246950201116367webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///tmp/anonymous5472246950201116367webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///tmp/anonymous5472246950201116367webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)

No comments:

Post a Comment