How to handle the "unexpected alert open"?

This should do the trick:

driver.switchTo().alert().accept();

Methods to handle alerts in Selenium

  1. Decide on each individually

If you need to take action on each alert in your tests individually, the driver gives you the option to switch to the alert and decide to either accept or dismiss it.

driver.switchTo().alert().accept();

  1. Handle by default setup

When you want all the alerts handled in the same way, you can set a global capability at the start of the test execution to ACCEPT, INGORE or DISMISS alerts by default when they appear.

capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);

  1. Using Robot class

Alternatively, you could use Robot class to send an Enter key event, which would accept the alert.

Robot r = new Robot();
 
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

Tags:

Java

Selenium