Okay Raza, if your aim is to check for an alert and accept it if it exists (as you mentioned that some pages are showing you the alert and some not); try using the following method see if this solves your problem:
public boolean isAlertPresent() throws InterruptedException {
try {
for(int i=1; i<3; i++){
TimeUnit.SECONDS.sleep(3);
getDriver().switchTo().alert();
System.out.printf("%n*** An alert was present ***");
alertPresent=true;
}
catch (NoAlertPresentException e) {
System.out.printf("%n*** An alert was not present ***");
alertPresent=false;
}
return alertPresent;
}
Now depending on whether the alert was present or not you can direct your flow, this is how you accept the alert (yes, ok, continue):
if (alertPresent==true){
driver.switchTo().alert().accept();
System.out.println("Successfully accepted an alert!");
}
else{System.out.println("Couldn't locate an alert!");}
Hope the above solves it for you; if not, no worries, post a msg below and we will see what we can do.
Best of luck!