There’s always a difficulty to handle Alerts in a Page.In that case we’ve to create a Method to handle alert and use Try and catch block.So that whenever an alert appears,it will handle.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class alerts_Ex
{
static FirefoxDriver fd=new FirefoxDriver();
public static boolean check_alert()
{
try
{
Alert a=fd.switchTo().alert();
String str=a.getText();
a.accept();
return true;
}
catch(Exception e)
{
System.out.println(“no alert”);
return false;
}
}
public static void main(String[] args)
{
fd.findElement(By.id(“find flights “)).click();
boolean x=check_alert();
fd.findElement(By.id(“continue”)).click();
}
}