Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I have the following code in a Selenium 2 Web Driver test which works when I am debugging but most of the time fails when I run it in the build. I know it must be something to do with the way the page is not being refreshed but do not know how to resolve it so any pointers as to what I have done wrong are appreciated. I am using JSF primefaces as my web application framework. When I click on the add new link a popup dialog box appears with an input box that I can enter a date into then click save. It is on getting the input element to enter text into that I get a stale element ref exception.

Thanks in advance

import static org.junit.Assert.assertEquals;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.junit.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.StaleElementReferenceException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;




public class EnterActiveSubmissionIntegrationTest {

Map<String, Map<String, String>> tableData = new HashMap<String, Map<String, String>>();


public void testEnterActiveSubmission() throws Exception {

    // Create a new instance of the Firefox driver

    // Notice that the remainder of the code relies on the interface, 

    // not the implementation.

    System.setProperty("", "C:/apps/chromedriver.exe");

    WebDriver driver = new ChromeDriver();

    // And now use this to visit Google


    // Alternatively the same thing can be done like this

    // driver.navigate().to("");

    // Find the text input element by its name

    WebElement element = driver.findElement(By.linkText("Manage Submissions"));;

    parseTableData(driver, "form:submissionDataTable_data", 1);

    assertEquals(tableData.get("form:submissionDataTable_data").get("12"), "Archived");

    WebElement newElement = driver.findElement(By.linkText("Add new"));;

    WebDriverWait wait = new WebDriverWait(driver,10);

    wait.until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver driver) {

            WebElement button = driver.findElement(By


            if (button.isDisplayed())

                return true;


                return false;



    WebElement textElement = driver.findElement("createForm:dateInput_input"));


    WebElement saveElement = driver.findElement("createForm:saveButton"));;


    parseTableData(driver, "form:submissionDataTable_data", 2);

    //Close the browser



private void parseTableData(WebDriver driver, String id, int expectedRows) {

    // Check the title of the page or expected element on page

    WebElement subTableElement = driver.findElement(;

    List<WebElement> tr_collection=subTableElement.findElements(By.xpath("id('"+ id + "')/tr"));

    assertEquals("incorrect number of rows returned", expectedRows, tr_collection.size());

    int row_num,col_num;


    if(tableData.get(id) == null) {

        tableData.put(id, new HashMap<String, String>());


    Map<String, String> subTable = tableData.get(id);

    for(WebElement trElement : tr_collection)


        List<WebElement> td_collection=trElement.findElements(By.xpath("td"));


        for(WebElement tdElement : td_collection)


            subTable.put(row_num + "" + col_num, tdElement.getText());







When I run this I get the following exception but it can occur on

WebElement textElement = driver.findElement("createForm:dateInput_input")); 


if (button.isDisplayed())

exception trace

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

(Session info: chrome=26.0.1410.64)

  (Driver info: chromedriver=0.8,platform=Windows NT 6.0 SP2 x86) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 56 milliseconds

For documentation on this error, please visit:

Build info: version: '2.32.0', revision: '6c40c187d01409a5dc3b7f8251859150c8af0bcb', time: '2013-04-09 10:39:28'

System info: 'Windows Vista', os.arch: 'x86', os.version: '6.0', java.version: '1.6.0_10'

Session ID: 784c53b99ad83c44d089fd04e9a42904

Driver info:

Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true,   browserName=chrome, rotatable=false, driverVersion=0.8, locationContextEnabled=true,  version=26.0.1410.64, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true,  browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true,   applicationCacheEnabled=false, takesScreenshot=true}]

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at  sun.reflect.NativeConstructorAccessorImpl.newInstance(

at  sun.reflect.DelegatingConstructorAccessorImpl.newInstance(

at java.lang.reflect.Constructor.newInstance(

at org.openqa.selenium.remote.ErrorHandler.createThrowable(

at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(

at org.openqa.selenium.remote.RemoteWebDriver.execute(

at org.openqa.selenium.remote.RemoteWebElement.execute(

at org.openqa.selenium.remote.RemoteWebElement.isDisplayed(

at com.integration.web.EnterActiveSubmissionIntegrationTest$1.apply(

at com.integration.web.EnterActiveSubmissionIntegrationTest$1.apply(


at com.integration.web.EnterActiveSubmissionIntegrationTest.testEnterActiveSubmission(

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

2 Answers

+1 vote
by (62.9k points)

Causes of Stale Element Reference Exception:

A stale element reference exception is thrown in one of two cases, the first being more common than the second.


The two reasons for Stale element reference are


  1. The element has been deleted entirely.
  2. The element is no longer attached to the DOM.

We face this stale element reference exception when the element we are interacting is destroyed and then recreated again. When this happens the reference of the element in the DOM becomes stale. Hence we are not able to get the reference to the element.

How To Overcome Stale Element Reference Exception in Selenium:

Solution 1:

You could refresh the page and try again for the same element.

I am taking a scenario that says you are trying to click on a link and getting the stale element exception.

Sample code to overcome the issue


driver.findElement(By.xpath("xpath here")).click();

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium online training!

0 votes
by (140 points)

Hi Mate, 

Even I faced the same issue and found a solution. It has 4 solutions. We can use waits, refresh the page, and using the page object model. I guess you can check this post for a better understanding of the Stale Element Reference Exception. I got the solution after googled a lot. Finally found this interesting website Software Testing Material.

Cheers Mate.

Browse Categories