What is Stale Element Reference Exception in Selenium Webdriver & How To Fix It?



We may encounter StaleElementReferenceException while working with Selenium webdriver. We can fix the StaleElementReferenceException in the Selenium webdriver. The term stale means something which is not fresh and decayed. Thus a stale element points to an element that is not present anymore.

There may be a case when an element was in DOM initially but after modifications in Document Object Model (DOM), the element becomes stale and the StaleElementReferenceException is thrown if we attempt to access this element.

This exception is caused whenever an element is not present in the DOM or deleted. We can handle this exception in the following ways −

  • Refreshing the page and verifying again.
  • Implement the retry method.

Example1

Code Implementation to illustrate StaleElementException.

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class StaleElmnt{    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       //implicit wait       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);       //application launch       driver.get("https://www.tutorialspoint.com/about/about_careers.htm");       // identify element       WebElement l = driver.findElement(By.id("gsc-i-id1"));       //enter text       l.sendKeys("Selenium");       //refresh page       driver.navigate().refresh();       //again enter text       l.sendKeys("Selenium");       //browser quit       driver.quit();    } }

Output

Example2

Code Implementation to fix the StaleElementException.

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.StaleElementReferenceException; public class StaleElmntFix{    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();       //implicit wait       driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);       //application launch       driver.get("https://www.tutorialspoint.com/about/about_careers.htm");       // identify element       WebElement l = driver.findElement(By.id("gsc-i-id1"));       //enter text       l.sendKeys("Selenium");       //refresh page       driver.navigate().refresh();       //fix exception with try-catch block       try{          l.sendKeys("Selenium");       }       catch(StaleElementReferenceException e){          l = driver.findElement(By.id("gsc-i-id1"));          l.sendKeys("Selenium");          //obtain value entered          String s= l.getAttribute("value");          System.out.println("Value entered is: " +s);       }       driver.quit();    } }

Output

Updated on: 2021-11-18T11:25:24+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements