How to overcome state Element Reference exception


I am trying to automate the following actions:

  1. Launching > Click on Mobiles > Mouse hover on Electronics and then click on Mi.

I am getting Expection in thread "main" state Element Reference: element is not attached to the page document in the miButton() method.

Please refer the error details section.


Mi Button Click – HTML

Base class:

public class Base {
        static WebDriver driver;
    public void setupBrowser(String browser, String url) {
        String currDir = System.getProperty("user.dir");
        if(browser.equalsIgnoreCase("chrome")) {
            System.setProperty("", currDir + "\\drivers\\chromedriver.exe");
            driver = new ChromeDriver();
        else if(browser.equalsIgnoreCase("firefox")) {
            System.setProperty("webdriver.gecko.driver", currDir + "\\drivers\\geckodriver.exe");
            driver = new FirefoxDriver();
        else if(browser.equalsIgnoreCase("edge")) {
            System.setProperty("webdriver.edge.driver", currDir + "\\drivers\\msedgedriver.exe");
            driver = new EdgeDriver();
        else {
            System.out.println("Valid browser not found therefore quitiing");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        if(url != "") 
    public void closeBrowser() {


Page Class

    public class pagetest extends Base{
        Actions action;
        public void closebtn() {
        public void mibutton() {
            WebElement mobiles = driver.findElement(By.xpath("//div[text()='Mobiles']"));  
            action = new Actions(driver);
            WebElement electronicsmenu = driver.findElement(By.xpath("//span[text()='Electronics']"));
            List <WebElement> value = driver.findElements(By.xpath("//div[@class='_1kidPb']/div[@class='_1QrT3s']//a");
            for(WebElement elem:value) {
                if(elem.getText().equals("Mi")) {
            WebElement label = driver.findElement(By.xpath("//p[text()='Latest from MI : ']"));
            System.out.println("The Label 'Latest from MI' is present : " +label.isEnabled());
    public static void main(String[] args) {
            pagetest obj = new pagetest();
            obj.setupBrowser("chrome", "");

Error Details

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=89.0.4389.90)
For documentation on this error, please visit:


You never find elements and then right away try to click on them. When the page is loading the elements are created, but after a few milliseconds they change their attributes, size and or location. This exception means that you found the element but as of now you "lost" it.

In order to avoid StateElementReferecneException use WebDriverWait object. You can wait for element to be visible / clickable as well as other options. Best option is to wait for the element to be clickable:

  WebDriverWait wait = new WebDriverWait(driver, 20, 10);        

Answered By – Tal Angel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published