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.
- JS Executor
- Robot class
- WebDriver Wait
- .click() method
- 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??!!...

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();
}