Saturday, January 23, 2016

How To Handle Stale Element Reference Exception in Selenium Webdriver

public boolean retryingFindClick() {
boolean result = false;
 int count = 0;
// It will try 2 times to find same element using name.
    while (count < 2) {
         try {
                //If exception generated that means It Is not able to find element
               //then catch block will handle It.
               WebElement webEle = driver.findElement(By.name("name"));
               //If exception not generated that means element found
               //and element text get cleared.
              webEle.click();
              result = true;
              break;
         } catch (StaleElementReferenceException e) {
                System.out.println("Trying to recover from a stale element :"
                                                 + e.getMessage());
         }
          count++;
    }
    return result;
}

4 comments:

  1. Hi Syam,

    I used retryingFindClick () function but I am getting these compilation errors.Can you please what is wrong in this code.

    ReplyDelete
  2. package Testselpackage1;

    import org.openqa.selenium.By;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.StaleElementReferenceException;
    import org.openqa.selenium.chrome.ChromeDriver;

    public class Testselclass1 {

    public static void main (String[]args){

    System.setProperty("webdriver.chrome.driver", "C:\\SeleniumDrivers\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.yahoo.com");
    driver.manage().window().maximize();
    driver.findElement(By.linkText("Mail")).click();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    WebElement username=driver.findElement(By.id("login-username"));
    username.sendKeys("n_ravikumar1");
    WebElement signin=driver.findElement(By.id("login-signin"));
    signin.click();

    public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
    try {
    driver.findElement(By.id(("login-passwd"))).sendKeys("Tiger#12");
    result = true;
    break;
    } catch(StaleElementException e) {
    System.out.println("Trying to recover from a stale element :"
    + e.getMessage());
    }
    attempts++;
    }
    return result;
    } /*driver.findElement(By.id("login-passwd"))
    /*password.sendKeys("Tiger12#");
    /*driver.findElement(By.id("login-signin")).click(); */
    }



    }

    ReplyDelete
    Replies
    1. Hi,

      driver.findElement(By.id(("login-passwd"))).sendKeys("Tiger#12");

      please remove () from the password locator in the above syntax. Use the below code.
      driver.findElement(By.id("login-passwd")).sendKeys("Tiger#12");

      Delete
  3. errors I am getting are,
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Illegal modifier for parameter retryingFindClick; only final is permitted
    Syntax error on token "(", ; expected
    Syntax error on token ")", ; expected
    StaleElementException cannot be resolved to a type
    Void methods cannot return a value

    at Testselpackage1.Testselclass1.main(Testselclass1.java:25)

    ReplyDelete