Reputation: 759
In selenium2 (Webdriver)How to verify if an alert is present? and continue doing something if its not present!!!
I am doing this:
driver.findElement(By.id("btn_may_or_maynot_showalert")).click();
WebDriverWait wait = new WebDriverWait(driver, 2);
try{
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
}
catch (Exception e){
System.out.println("No alert");
}
driver.findElement(By.id("Cont_doing_something")).click();
This works fine But is there a better way?
Upvotes: 3
Views: 19511
Reputation: 27486
No, you're doing things the way the library expects you to. However, one of the principles of the library is that you should always know what to expect of your automation code. That means you shouldn't run into an instance where the button "may or may not" cause an alert; you should already know whether pressing the button will cause an alert or not. If it does something other than what you expect, that's an exceptional condition, and an exception should be thrown.
Upvotes: 2