1

I am testing the newly built framework, and am often encountering org.openqa.selenium.StaleElementReferenceException while working in the Chrome browser. Could there be an issue with the framework design? There are no issues when I run my tests in other browsers. I tried many types of custom waits that catch the StaleElementReferenceException and then loops to find the element, but no luck.

Has anyone faced a similar issue and found a solution?

Chrome version: 38.0.2125.111
Selenium version: 2.43.1

public WebElement waitTill(By by){
    WebElement ele = null;
    for(int i=0; i<15; i++){
        try {
            ele = driver.findElement(by);
            if(ele==null)
                Thread.sleep(2000); //in last attempt used thread...we wont use this in actual practice
                else
                    break;
        } catch (NoSuchElementException | InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }
    return ele;
}

public WebElement getElement(String loc) {
    String locator = initUtils.ORProp.getProperty(loc);
    WebElement element = null;
    try{
        By by = getBy(locator);
        element = waitTill(by);
    }catch(NoSuchElementException e){
        System.out.println(e.getMessage());
    }catch(StaleElementReferenceException e){
        By by = getBy(locator);
        element = waitTill(by);
    }
    return element;
}
2
  • 1
    Difficult to say without having an actual code and a complete traceback. Usually, the error happens when you are manipulating the element which is no longer linked to the page, the page was changed after you found the element. Commented Nov 10, 2014 at 20:08
  • @alecxe- Thanks for the replay. In that case how come it is working fine with other browsers? Why only it is happening with chrome? Commented Nov 10, 2014 at 20:09

2 Answers 2

3

This sort of error is caused by the webpage changing in between times the element is checked.

This exception is mostly caused by bad tests I am afraid to say. A few things to check for are:

  • Make sure that you are giving the page time to load when you go to it before you start interacting with it, even if you think it has finished loading it may still be waiting for something in the background and when it arrives the page changes.

  • Make sure that if you interact with any element that changes the page make sure you again wait for the page to change and any html requests to process.

These two things may be the cause of most of your problems, I would recommend using a ajax that uses jQuery ajax start and stop in order to make sure that the page is loaded before modifying it. What you need to remember is selenium is so much faster than a user could possibly interact with the page and you need to handle that by increasing checks.

I would also recommend checking whether an element is on the page and that it is visible before even trying to interact with it.

In a worse case senario you could use a try and catch block to check for the element but if you make sure the page is not changing then you shouldnt get the exception. It does differ between browsers due to browser speed and webdriver speed.

Some of the code I use is:

var finished = false;

function ready() {
  if (finished == true) {
    $( "#main" ).addClass("ready");
  }
}

$( document ).ajaxStart(function() {
  $( "#main" ).removeClass("ready");
  finished = false;
});

$( document ).ajaxStop(function() {
  finished = true;
  window.setTimeout(ready,500);
});'

This checks that the page is fully loaded and no requests are pending, I just execute this once the browser is open, I then can just check whether the class is present and if it is I am ready to go. I call the same check whenever the page changes as well.

Sign up to request clarification or add additional context in comments.

3 Comments

@Zenon-thnk u. I have tried with number of assertions and also finding elements with try catch. We have util method which will find an element using the properties file key. I have also added that code above (modified waits method several times, now the one which attached is the latest one) ...will be helpful if could you share your views
I personally hate having timed waits where they are not needed, telling the web driver to wait lets say 3 seconds is not so great as it wont necessarily wait long enough or it may be waiting to long. Some of the code I use for testing a page is ready to go is: 'var finished = false;' 'function ready() {' ' if (finished == true) {' ' $( "#main" ).addClass("ready");' ' }' '}' '$( document ).ajaxStart(function() {' ' $( "#main" ).removeClass("ready");' ' finished = false;' '});' '$( document ).ajaxStop(function() {' ' finished = true;' ' window.setTimeout(ready,500);' '});'
Seems in you code that you are waiting for it after you get the stale element exception, you should sleep for a certian amount of time, then try and find it again, you may get a staleReferenceException again if the page is still changing. You are not catching that in your waitTill method.
1

Programmatically, I might use an explicit wait with ExpectedConditions instead of Thread.sleep():

    public WebElement element2Click (By by){
      WebElement myClickableElement = (new WebDriverWait(driver, 10))
      .until(ExpectedConditions.elementToBeClickable(by));

      return myClickableElement;
}

But if this does not solve the problem and your automated test program is quite stable across the other browsers, it could be that the web page or webapp you are testing does not support Chrome, and you may be seeing StaleElementReferenceException errors because the HTTP messages are not communicating properly. In this case, check out the HTTP traffic on the browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.