try{
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.alertIsPresent());
System.out.println(driver.switchTo().alert().getText());
driver.switchTo().alert();
}
catch (NoAlertPresentException Ex)
{
System.out.println("No alert");
}