4

I am completely new to automation testing. After referring some tutorials, I have created an automated test case. The test case I try to automate is to check whether the sorting worked correctly after I click on one of the headers of a table.

My automated test case fails with the following exception:

org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
Command duration or timeout: 12 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:08:56'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.35-30-generic',     java.version: '1.6.0_20'
Driver info: driver.version: RemoteWebDriver
Session ID: 95b80ea0-26ac-45e0-a407-79f5b687504a
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at  sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:244)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:169)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:240)
at org.openqa.selenium.By$ByTagName.findElements(By.java:312)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:151)
at Sorting.sorting_column(Sorting.java:68)

Following is the code:

        //Fetching values from the column of a table
    WebElement table = driver.findElement(By.id("dnn_ctr381_View_DealerList_ctl00"));
    List<WebElement> rows = table.findElements(By.tagName("tr"));

    for (WebElement row : rows) {

        List<WebElement> cells = row.findElements(By.tagName("td"));
        if (!cells.isEmpty() && cells.get(0).isDisplayed()) {
            a = cells.get(0).getText();
        }
        b[i] = a;
        i++;
    }
    //Click on column header to sort   
    driver.findElement(By.cssSelector("html body#Body form#Form div#container.container div#Wrapper div#Main div#Panes div#ContentsContainer.box-shadow div#Contents div#Content div#dnn_ContentPane.MainContent div.DnnModule div.dnnPEMContNotitle div#dnn_ctr381_ContentPane.dnnPEMContNotitleBody div#dnn_ctr381_ModuleContent.DNNModuleContent div#dnn_ctr381_View_dnn_ctr381_View_DealerListPanel div#dnn_ctr381_View_DealerList.RadGrid table#dnn_ctr381_View_DealerList_ctl00.rgMasterTable thead tr th.rgHeader a")).click();
    WebElement table1 = driver.findElement(By.id("dnn_ctr381_View_DealerList_ctl00"));
    List<WebElement> rows1 = table1.findElements(By.tagName("tr"));
    for (WebElement row1 : rows1) {
        List<WebElement> cells1 = row1.findElements(By.tagName("td"));// Exception here
        if (!cells1.isEmpty() && cells1.get(0).isDisplayed()) 
            {

         c = cells1.get(0).getText();

       }

Exception is from this line:

List<WebElement> cells = row1.findElements(By.tagName("td"));

Can someone please let me know the cause of this isse and how it can be resolved?

Any help is appreciated

user1400538
  • 855
  • 5
  • 24
  • 42
  • A lot of discussion over at http://stackoverflow.com/questions/16166261/selenium-webdriver-stale-element-reference-exception. Possible dupe? – Rob Barreca Aug 09 '13 at 00:29

4 Answers4

9

StaleElementException is there to tell you that a DOM element that you've referenced in your client code (by a findElement) doesn't exist anymore. In your case, the row1 element doesn't exist anymore. It is certainly a timing issue, as your click to sort is just above, and no care is taken to ensure the sort has finished. Without more insight in how the sort is implemented, or how long it takes, it'd be my best guess. If the sort ends while you iterate through your DOM objects, these will be rearranged in the DOM and lost in your client code

Grooveek
  • 10,046
  • 1
  • 27
  • 37
5

Well, StaleElement exception can be really frustrating..escpecially while working with chrome driver...but as of now the only way to get around is simply using what I call a Raw wait:

try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Though the purists might say that we are using magic numbers but once you know the safe waiting magnitude then I have known it to be effective in more than 95% cases.

Anirudh
  • 2,286
  • 4
  • 38
  • 64
2

Use webDriverwait and make the driver to wait until the element is located on the dom.

WebDriverWait wait = new WebDriverWait(driver,20);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*

[@id='mndf']/form/fieldset/table/thead/tr/th")));

Use an output statement whether the element is located or not.

System.out.println("element located");

I hope this helps.

benka
  • 4,732
  • 35
  • 47
  • 58
Rami
  • 21
  • 1
0

It is to do with DOM changing becuase of some javascript getting executed when an interaction happens on the page. So as the error says element has become stale so solution is to do driver.findElement again to get rid of it. Faced same issue and resolved it

suprinder
  • 11
  • 3