DEV Community

Cover image for ๐Ÿ”ฅ How To Handle Stale Element Exception in Selenium(With Code)
Pramod Dutta
Pramod Dutta

Posted on • Updated on

๐Ÿ”ฅ How To Handle Stale Element Exception in Selenium(With Code)

โœ… Join us - https://sendfox.com/thetestingacademy

In this video, We are going to learn How To Handle Stale Element Exception in Selenium(with code)

๐Ÿš€ Day 30 Task: Stale Element Exception in Selenium(With Code).
๐Ÿš€ All Task List: https://scrolltest.com/automation/task
๐Ÿš€ Watch Full Playlist: https://apitesting.co/30days
๐Ÿš€Download MindMap: https://scrolltest.com/Titb

 Handle Stale Element Exception in Selenium

โœ… What is StaleElementReferenceException?
It means decay, old and no fresh, It basically means element is no longer available in DOM.
If the DOM changes then the WebElement goes stale.
If you are trying to access the element, it will give - StaleElementReferenceException

โœ… Causes of Stale Element Reference Exception
element is no longer attached to the DOM.
element has been deleted entirely.

โœ… How To Handle Stale Element Reference Exception in Selenium

  1. Page Refresh and Try to check again.
  2. Re try(with for loop)

๐Ÿ‘ช Join our Community - http://bit.ly/learntesting2019
โœ… Automation Tester Community - https://thetestingacademy.com
๐ŸฆFollow us on Twitter - https://twitter.com/itstechmode
๐Ÿ“– Like us on Facebook - https://www.facebook.com/scrolltest

๐ŸŽค Listen to our Podcast - https://anchor.fm/thetestingacademy

automationtesting #manualtesting #testautomation #thetestingacademy #scrolltest

package com.scrolltest;

import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class DDTTry {

    private WebDriver driver;
    private XSSFWorkbook workbook;
    private XSSFSheet sheet;
    private XSSFCell cell;

    public DDTTry() {

    }


    @Test(priority = 0)
    public void testFullPage() throws IOException {

        File file = new File("src/main/java/com/scrolltest/TD.xlsx");
        FileInputStream fis = new FileInputStream(file);
        workbook = new XSSFWorkbook(fis);
        sheet = workbook.getSheetAt(0);
        for (int i = 1; i <= sheet.getLastRowNum( ); i++) {

            cell = sheet.getRow(i).getCell(0);
            DataFormatter formatter = new DataFormatter( );
            String username = formatter.formatCellValue(cell);
            cell = sheet.getRow(i).getCell(1);
            String password = formatter.formatCellValue(cell);
            for (int retry = 0; retry < 5; i++) {
                try {
                    driver.findElement(By.name("userName")).sendKeys(username);
                } catch (StaleElementReferenceException ex) {
                    System.out.println(ex.toString());
                }
            }
            driver.findElement(By.name("password")).sendKeys(password);
            driver.findElement(By.name("submit")).click( );
            driver.manage( ).timeouts( ).implicitlyWait(3, TimeUnit.SECONDS);
            Assert.assertTrue(driver.findElement(By.linkText("SIGN-OFF")).isDisplayed( ));
            driver.findElement(By.linkText("SIGN-OFF")).click( );

        }

    }

    @BeforeTest
    public void beforeTest() {
        driver = new FirefoxDriver( );
        driver.get("http://demo.guru99.com/test/newtours/index.php");
        driver.manage( ).window( ).maximize( );
        driver.manage( ).timeouts( ).implicitlyWait(5, TimeUnit.SECONDS);

    }

    @AfterTest
    public void afterTest() {
        driver.quit( );
    }

}

--
Be sure to subscribe for more videos like this!

 TheTestingAcademy

Top comments (0)