try this
[quote:wazpqetn]
public Alert clickAndWaitTillAlert(String elementtobeClicked)
{
Alert al=null;
try{
for(int i=0;i<5;i++)
{
driver.findElement(By.xpath(elementtobeClicked)).click();
}catch(Exception e)
{
}
try
{
al=driver.switchTo().alert();
return al;
}catch(Exception e)
{
try {
Thread.sleep(3000L);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
return al;
}[/quote:wazpqetn]