javaselenium

How to handle the "unexpected alert open"?


I got an issue with Selenium throwing timeout exception because of a pop up window

  unexpected alert open
  not provide any stacktrace information)
  Command duration or timeout: 5 milliseconds

The alert has OK and CANCEL buttons. I know two ways to handle this


The first way is reopen a new session

driver.quit();
driver = new ChromeDriver();

Second way is using Robot class

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

However, this methods are not time efficient. Is there any better way?


Solution

  • This should do the trick:

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