Snow Gift
Snow Gift

Reputation: 39

Exception: org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

It shows the Stale Element Reference Exception when I try to check whether the alert is displayed or not. I have used all the following techniques to handle the Alert dialogue box but it shows an Exception.

  1. JS Executor
  2. Robot class
  3. WebDriver Wait
  4. .click() method
  5. Action class

All the Elements are present while I check manually. When I automate it, it shows an exception... But I don't know where it occurs??!!... Login Module

Step Definition Code:

(In this code, I am using Action class)

 public void alert_msg_display() throws Throwable {
        WebElement x= driver.findElement(By.xpath("//button[@data-hover='LOGIN NOW']")); //Login button path
        actionClick(driver, x);
        WebElement y= driver.findElement(By.xpath("//md-dialog[@aria-label='Alert']/md-dialog-content/div")); // Alert message text path
        String a= y.getText();
        WebElement z= driver.findElement(By.xpath("//button[@ng-click='hide()']")); // Alert box close button path
        actionClick(driver, z);
        String a1 = "Please Enter Branch Id";
        driver.findElement(By.xpath("//input[@ng-model='Branchid']")).sendKeys("HO");
        actionClick(driver, x);
        String b= y.getText();
        actionClick(driver, z);
        String b1 = "Please Enter Username (Email Id)";
        driver.findElement(By.xpath("//input[@ng-model='Username']")).sendKeys("testmail"); 
        actionClick(driver, x);
        String c= y.getText();
        actionClick(driver, z);
        String c1 = "Please Enter Username (Email Id)";
        driver.findElement(By.xpath("//input[@ng-model='Username']")).clear();
        driver.findElement(By.xpath("//input[@ng-model='Username']")).sendKeys("[email protected]");
        actionClick(driver, x);
        String d= y.getText();
        actionClick(driver, z);
        String d1 = "Please Enter Password";
        driver.findElement(By.xpath("//input[@name='password']")).sendKeys("abcde");
        actionClick(driver, x);
        String e= y.getText();
        actionClick(driver, z);
        String e1 = "LOGIN FAILED, PLEASE CHECK YOUR USERNAME OR PASSWORD";
       if (a.equals(a1) && b.equals(b1) && c.equals(c1) && d.equals(d1) && e.equals(e1))
            test.log(LogStatus.PASS, "Test Case ID: LOG_006 to LOG_010 - Pass");
        else
            test.log(LogStatus.FAIL, "Test Case ID: LOG_006 to LOG_010 - Fail");
    }

Runner File Code

public void actionClick(WebDriver driver, WebElement a) {
    Actions action = new Actions(driver);
    action.moveToElement(a).click().build().perform();
}

Upvotes: 0

Views: 179

Answers (1)

supputuri
supputuri

Reputation: 14135

The movement you click on 'Login Now' (x) element the references to x & y will be refreshed. So you can't use the x & y that are pointing to the old references and that will lead to the Statle Element exception.

The solution would be to get the element each time you want to click on the x & y so that you will have the latest reference to the elements.

Upvotes: 0

Related Questions