Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How to handle StaleElementReferenceException Through Page Object Model – PageFactory – Make Selenium Easy

Posted on 03/19/2020 By admin

Hello Folks,

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

I am trying to automate below steps:

  1. Load URL “https://github.com/login”.
  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:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class StaleElementHandling {

 public static void main(String[] args) throws InterruptedException {
 
 System.setProperty(“webdriver.chrome.driver”, “./exefiles/chromedriver.exe”);
 WebDriver driver= new ChromeDriver();
 // Open URL
 driver.get(“https://github.com/login”);
 
 // locate and type username
 WebElement username= driver.findElement(By.id(“login_field”));
 username.sendKeys(“amod”);
 
 // locate and type password
 WebElement pass= driver.findElement(By.id(“password”));
 pass.sendKeys(“amod”);
 
 // locate and click on submit
 WebElement sub= driver.findElement(By.xpath(“//input[@value=’Sign in’]”));
 sub.click();
 
 Thread.sleep(10000);
 
 // again type username
 username.sendKeys(“amod”);
 
 }
}


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class StaleElementHandling {

 public static void main(String[] args) throws InterruptedException {
 
 System.setProperty(“webdriver.chrome.driver”, “./exefiles/chromedriver.exe”);
 WebDriver driver= new ChromeDriver();
 // Open URL
 driver.get(“https://github.com/login”);
 
 // locate and type username
 WebElement username= driver.findElement(By.id(“login_field”));
 username.sendKeys(“amod”);
 
 // locate and type password
 WebElement pass= driver.findElement(By.id(“password”));
 pass.sendKeys(“amod”);
 
 // locate and click on submit
 WebElement sub= driver.findElement(By.xpath(“//input[@value=’Sign in’]”));
 sub.click();
 
 Thread.sleep(10000);
 
 // again type username
 username.sendKeys(“amod”);
 
 }
}


Output:

We get StaleElementReferenceException at line

 username.sendKeys(“amod”);

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:

LoginPage.java

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {

 WebDriver driver;
 @FindBy(id=”login_field”)
 public WebElement username;
 
 
 @FindBy(id=”password”)
 public WebElement password;
 
 @FindBy(xpath=”//input[@value=’Sign in’]”)
 public WebElement submit;
 
 public LoginPage(WebDriver driver)
 {
 this.driver=driver;
 PageFactory.initElements(driver, this);
 }
}


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {

 WebDriver driver;
 @FindBy(id=”login_field”)
 public WebElement username;
 
 
 @FindBy(id=”password”)
 public WebElement password;
 
 @FindBy(xpath=”//input[@value=’Sign in’]”)
 public WebElement submit;
 
 public LoginPage(WebDriver driver)
 {
 this.driver=driver;
 PageFactory.initElements(driver, this);
 }
}


StaleElementHandling2.java

import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class StaleElementHandling2 {

 public static void main(String[] args) throws InterruptedException {
 
 System.setProperty(“webdriver.chrome.driver”, “./exefiles/chromedriver.exe”);
 WebDriver driver= new ChromeDriver();
 driver.get(“https://github.com/login”);
 
 LoginPage lp= new LoginPage(driver);
 lp.username.sendKeys(“amod”);
 lp.password.sendKeys(“dsds”);
 lp.submit.click();
 Thread.sleep(5000);
 lp.username.sendKeys(“amod”);
 lp.password.sendKeys(“dsds”);
 lp.submit.click();
 
 
 }
}


import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class StaleElementHandling2 {

 public static void main(String[] args) throws InterruptedException {
 
 System.setProperty(“webdriver.chrome.driver”, “./exefiles/chromedriver.exe”);
 WebDriver driver= new ChromeDriver();
 driver.get(“https://github.com/login”);
 
 LoginPage lp= new LoginPage(driver);
 lp.username.sendKeys(“amod”);
 lp.password.sendKeys(“dsds”);
 lp.submit.click();
 Thread.sleep(5000);
 lp.username.sendKeys(“amod”);
 lp.password.sendKeys(“dsds”);
 lp.submit.click();
 
 
 }
}


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.

#HappyLearning

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

Uncategorized

Post navigation

Previous Post: Untitled Diagram (7) – Make Selenium Easy
Next Post: Css – Make Selenium Easy

Related Posts

April 23, 2017 – Make Selenium Easy Uncategorized
SelectMethod – Make Selenium Easy Uncategorized
June 9, 2017 – Make Selenium Easy Uncategorized
Amod Mahajan Uncategorized
Part 3: Handling Multi Select Drop-down Created Using SELECT Tag Uncategorized
YatraCal – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark