StaleElementReference Exception in PageFactory


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?

  • You must to post comments


The documentation of StaleElementReferenceException here clearly indicates the common reasons as follows:

  • The element has been deleted entirely : The most common reason is that page that the element was part of has been refreshed, or the user has navigated away to another page. There is also a possibility that a JS library has deleted an element and replaced it with one with the same ID or attributes.

  • The element is no longer attached to the DOM : In this case, a Web Application with tabbed UI framework can be implemented through DIVs for each tab, but only attach one at a time, storing the rest in variables. In this case, it’s entirely possible that your code might have a reference to an element that is no longer attached to the DOM.

Now, let me try to address your Questions one by one:

  1. when we do a initElements, the WebElements are located : When we 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 WebElements defined in LoginPageNew.class whenever and where ever it is mentioned within your Automation Script.

  1. I click on a webelement and because of which there is a change in one of the other webelements in DOM : This is not always true. A TextBox is also a WebElement and on clicking on a Textbox there will be no change of any WebElement or the HTML DOM. Yes, some of the WebElements does changes due to click on some other WebElement. This because some WebElements are associated with onClick() event. That means, when you click on those WebElements with onClick() or other similar type of events, either we are redirected to some other URI or some other WebElements appear/disappear from the HTML DOM. Hence a change in the HTML DOM results in change of the other WebElements.


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.
Let me know if this Answers your Question.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.