The idea is when you deal with alerts you have to check whether alert is present first.
I would use this approach:
public boolean isAlertPresent() {
boolean presentFlag = false;
try {
// Check the presence of alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
} catch (NoAlertPresentException ex) {
// Alert not present
ex.printStackTrace();
}
return presentFlag;
}
here you can get details Also do not forget about debug step by step to get to know on what step alert appears/not appears.
Hope this helps you.