By user2356679

2017-06-30 04:18:53 8 Comments

I am trying to learn the PageFactory model. I understood the fact that when we do a initElements, the WebElements are located. Say for example, I click on a webelement and because of which there is a change in one of the other webelements in DOM. Now, obviously I would get a StaleElementReferenceException here. How would I resolve this issue?

Should I find that specific WebElement again knowing the fact that there can be a change in the WebElement's properties in the DOM? or is there an another way to handle this?


@DebanjanB 2017-06-30 11:00:30


StaleElementReferenceException extends WebDriverException and indicates that the previous reference of the element is now stale and the element reference is no longer present on the DOM of the page.

Common Reasons

  • The common reasons behind facing StaleElementReferenceException are as follows:
    • The element has been deleted entirely.
    • The element is no longer attached to the DOM.
    • The webpage on which the element was part of has been refreshed.
    • The (previous) element has been deleted by a JavaScript or AjaxCall and is replaced by a (new) element with the same ID or other attributes.
  • Solution : If an (old) element has been replaced with new identical one, the simple strategy would be to use findElement() or findElements to look out for the element again.

Answering your queries

  1. When we do a initElements, the WebElements are located : When you call initElements() method, all the WebElements of that page will get initialized. For example,

    LoginPageNew login_page = PageFactory.initElements(driver, LoginPageNew.class);

    This line of code will initialize all the static WebElements defined within the scope of the LoginPageNew.class whenever and wherever it is invoked from your Automation Script.

  2. I click on a webelement and because of which there is a change in one of the other webelements in DOM : This is pretty much possible.

    • As an example, in general invoking click() on a <input> tag wouldn't trigger any change of any of the WebElements on the HTML DOM.
    • Where as invoking click() on a <button> tag or <a> tag may call a JavaScript or a Ajax which inturn may delete an element or can replace the (previous) element by a (new) element with the same ID or other attributes.


So, if WebDriver throws a StaleElementReferenceException, that implies even though the element still exists, the reference is lost. We should discard the current reference we have and replace it by locating the WebElement once again when it gets attached to the DOM. That means you have to again reinitialize the class through initElements() method which inturn reinitializes all the WebElements defined in that page.


If a old element has been replaced with new identical one, the simple strategy would be to invoke WebDriverWait inconjunction with ExpectedConditions to look out for the element.

You can find relevant detailed discussions in:


Here are the references of this discussion:

@Breaks Software 2017-06-30 12:02:56

Actually, it's not necessary to reinitialize your page class. it is sufficient to search for the specific element again.

@Breaks Software 2017-06-30 12:32:30

The downvote is because you do not have to reinitialize the entire page. As your quote from the documentation, and other answers, have mentioned, you can simply search for the element again. It is not necessary to call initElements() on your class.

@Breaks Software 2017-06-30 12:49:15

perhaps we should move further discussion to chat, but if you read the suggestions at it will be more clear to you. There are many good code samples in Stackoverflow, including one below.

@DebanjanB 2017-06-30 12:56:33

@BreaksSoftware I think you are missing the concept of PageFactory while you reply. The entire Answer is based on the concept from the URL I mentioned combining with Stackoverflow Answers only. I would request you to share your research on how do you search for an element which throws StaleElementReferenceException again through PageFactory without reinitializing the class?

@Breaks Software 2017-07-02 14:10:23

ah...My brain was reading "page object model", not "PageFactory model". This restriction of the PageFactory is probably why I abandoned using it years ago.… has a less than satisfactory alternate approach to the problem. DebanjanB, stackoverflow won't let me un-do my downvote unless the answer has been edited, so if you make some minor edit to just trigger that mechanism, I can undo the downvote.

@DebanjanB 2017-07-03 02:54:44

@BreaksSoftware Glad to hear that. Thanks

@Rehan Shikkalgar 2017-12-21 12:03:26

Worked when reinitialized the page class :)

@Ardesco 2019-03-29 16:10:08

This is a really bad answer that leads you down the wrong path whilst appearing to fix the issue. If you haven't used a @CachLookup annotation you don't need to re-initialize the class, you just use the WebElement again and the Java Proxy class that binds the WebElement to the element in the DOM will deal with finding the element again. Re-initiallizng and then using the WebElement again may appear to work, but it's not because you re-initialized the class, it's because you searched for the element again.

@Ardesco 2019-03-09 19:46:48

This is a known problem with the PageFactory implementation.

If you are unlucky enough for the element to become stale in the instant between the element being found, and then the element being clicked upon, you will get this error. Unfortunately the PageFactory code does not try to find the element again if it has become stale and it throws an Exception.

I would classify this as a bug with PageFactory, it should auto re-find the element if it ever becomes stale (unless the @CacheLookup annotation is used).

The suggestion to recall initElements isn't going to fix anything, you only need to init the elements once because that binds a Java proxy class to the element in question. The page factory implementation is supposed to remove the possibility of StaleElementReferenceExceptions (hence why this is a bug)

@Shailendra Rathore 2017-06-30 05:07:48

Stale element exception is thrown in two cases

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

When this happen you wrap your code in try catch block then you can loop and retry as many times as you need until it succeeds.

public void waitForElementPresent(final By by, int timeout){ 
  WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout)
  wait.until(new ExpectedCondition<Boolean>(){ 
    public Boolean apply(WebDriver webDriver) { 
      WebElement element = webDriver.findElement(by); 
      return element != null && element.isDisplayed(); 

@Andy Turfer 2019-01-15 15:27:15

Nice, but the solution specifically asks about dealing with stale element exceptions when using a Page Factory.

Related Questions

Sponsored Content

5 Answered Questions

[SOLVED] Get the By locator of an already found WebElement

4 Answered Questions

1 Answered Questions

[SOLVED] Pagefactory throwing null in Page Object Structure

2 Answered Questions

3 Answered Questions

5 Answered Questions

[SOLVED] Selenium Webdriver and PageFactory initialize List<WebElement> elements

1 Answered Questions

Getting stale element exception while iterating over element loop

2 Answered Questions

1 Answered Questions

Selenium WebDriver - StaleElementReferenceException that shouldn't be happening

  • 2012-10-25 19:56:16
  • James Martineau
  • 2001 View
  • 2 Score
  • 1 Answer
  • Tags:   selenium webdriver

Sponsored Content