How to handle StaleElementReferenceException Through Page Object Model(POM)

Hello Folks,

This is on demand post. Many people asked me how to handle StaleElementReferenceException through Page Object Model(POM). So thought of writing a simple post on it.

I am trying to automate below steps:

  1. Load URL “”.
  2. Type wrong username.
  3. Type wrong password.
  4. Click on submit.
  5. Type correct username.
  6. Type correct password.
  7. Click on submit.

Run the below program:


We get StaleElementReferenceException at line


What is StaleElementReferenceException and when it occurs?

Indicates that a reference to an element is now “stale” or the element no longer appears on the DOM of the page. Let’s understand this with real time example:-

You stay on a rented house in Silk board. All bank statements, documents are sent to your Silk board address. After some time you shift to BTM from Silk board. All documents sent to your Silk board address will be returned back as you do not stay there any more. You need to update latest address everywhere so that you will get all documents at your new address BTM.

In above example, we typed username, password and clicked on submit button. Page reloads and shows you error message “Incorrect username or password.” with login page as previous. Same page is shown again but address is changed. Address is referred as reference in JAVA. We are trying to use old reference and getting StaleElementReferenceException.

This is a major exception and you will face it always if you do not follow Page Object Model.

How Page Object Model solves StaleElementReferenceException?

Create two java classes as below:

You will see programs run fine when we use page object design pattern. You will never get Stale Element Reference Exception. When we use FindBy annotation in POM, webdriver locates element and update reference every time before performing any action on that. You can use web element without relocating any where. This is major advantage of using Page Object Model.

That’s it. Hope, it might be useful for people who face stale exception. If you have any doubt, feel free to comment.

If you like my posts, please like, share, comment and subscribe.



Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning