Selenium "NoSuchAlertError: no alert open" error
Asked Answered
R

6

8

In Selenium, I click a button and an alert box appears, and I tried to accept the alert box, but it gives me an error.

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

It's very inconsistent... Sometimes the tests pass, sometimes it gives me the error...

The error looks like:

NoSuchAlertError: no alert open
  (Session info: chrome=34.0.1847.131)
  (Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.9.2 x86_64)
      at new bot.Error (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/atoms/error.js:109:18)
      at Object.bot.response.checkResponse (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/atoms/response.js:106:9)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/webdriver.js:276:20
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:375:7)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1465:10
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:375:7)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:375:7)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:678:49
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/http/http.js:96:5
      at IncomingMessage.<anonymous> (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/http/index.js:114:7)
      at IncomingMessage.EventEmitter.emit (events.js:117:20)
      at _stream_readable.js:920:16
      at process._tickCallback (node.js:415:13)
  ==== async task ====
  WebDriver.switchTo().alert()
      at webdriver.WebDriver.schedule (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/webdriver.js:267:15)
      at webdriver.WebDriver.TargetLocator.alert (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/webdriver.js:1429:27)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/test/bandbooking/form.js:87:39
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:375:7)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:375:7)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at then (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:478:7)
      at Object.webdriver.promise.asap (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:723:11)
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1609:25)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at then (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:478:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:369:18)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at then (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:478:7)
      at Object.webdriver.promise.asap (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:723:11)
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1609:25)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at then (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:478:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:369:18)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1465:10
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at then (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:478:7)
      at Object.webdriver.promise.asap (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:723:11)
      at newFrame.then.e (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1614:25)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:375:7)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at webdriver.promise.ControlFlow.resolveFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1519:9)
      at webdriver.promise.ControlFlow.getNextTask_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1488:12)
      at webdriver.promise.ControlFlow.runEventLoop_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1446:59)
      at wrapper [as _onTimeout] (timers.js:252:14)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
  ==== async task ====
      at Context.<anonymous> (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/testing/index.js:126:14)
      at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:196:15)
      at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)
      at /usr/local/lib/node_modules/mocha/lib/runner.js:452:12
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)
      at /usr/local/lib/node_modules/mocha/lib/runner.js:309:7
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:247:23)
      at /usr/local/lib/node_modules/mocha/lib/runner.js:271:7
      at done (/usr/local/lib/node_modules/mocha/lib/runnable.js:185:5)
      at /usr/local/lib/node_modules/mocha/lib/runnable.js:199:9
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/testing/index.js:92:5
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:375:7)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1465:10
      at Object.webdriver.promise.asap (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:732:5)
      at newFrame.then.e (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1614:25)
      at /Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/goog/base.js:1178:15
      at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1598:20)
      at notify (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:428:12)
      at notifyAll (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:397:7)
      at resolve (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:375:7)
      at fulfill (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:493:5)
      at webdriver.promise.ControlFlow.resolveFrame_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1519:9)
      at webdriver.promise.ControlFlow.getNextTask_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1488:12)
      at webdriver.promise.ControlFlow.runEventLoop_ (/Users/kstrife/Desktop/experiments/seleniumNode/selenium-webdriver/lib/webdriver/promise.js:1446:59)
      at wrapper [as _onTimeout] (timers.js:252:14)
      at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Ruckus answered 7/5, 2014 at 20:54 Comment(0)
E
7

The documented solution doesn't work.

// this is the documented solution, which doesn't work
driver.switchTo().alert().dismiss().then(null, function(e) {
  if (e.code !== webdriver.ErrorCode.NO_SUCH_ALERT) {
    throw e;
  }
});

It just throws NoSuchAlertError: no alert open no matter what. This modified method however, works:

driver.switchTo().alert().thenCatch(function (e) {
    // 27 maps to bot.ErrorCode.NO_MODAL_DIALOG_OPEN: http://selenium.googlecode.com/git/docs/api/javascript/source/lib/atoms/error.js.src.html#l31
    if (e.code !== 27) { throw e; }
})
.then(function (alert) {
    if (alert) { return alert.dismiss(); }
});
  • This tries to switch to an open alert.

  • If no alert is found, handles the NoSuchAlertError exception but throws other errors up (since we don't want to gobble up every error).

  • If found, then calls dismiss to dismiss it. Feel free to call accept if you want.

Note: The position of thenCatch is crucial. This is against reasoning and against their documented control flow, but this is what works as of today.

Edge answered 5/9, 2014 at 20:41 Comment(2)
This does not work for protractor. I replaced the driver to protractor.getInstance() that does not help. Would you know how to do thatMidyear
Sorry, no idea about protractor.Edge
S
4

Instead of going for Thread.sleep, you can use explicit wait for alert, like following:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.alertIsPresent());

and then accept alert:

driver.switchTo().alert().accept();
Shelleyshellfire answered 8/5, 2014 at 6:1 Comment(2)
for those using webdriver / node.js - try driver.wait(webdriver.until.alertIsPresent())Hoebart
Not working for me to use .alertIsPresent() as asked here https://mcmap.net/q/1325541/-cannot-get-alert-box-in-selenium-webdriver-with-chrome/248616Lialiabilities
R
1

I just sleep for 1 second before I accept it.

element.click();
driver.sleep(1000);
driver.switchTo().alert().accept();
Ruckus answered 7/5, 2014 at 21:15 Comment(0)
B
1

You might as well change it to:

element.click();
while (true)
{
    try
    {
        driver.switchTo().alert().accept();
        break;
    }
    catch (NoSuchAlertError error)
    {
        Thread.sleep(100);
    }
}

This will:

  1. Keep you on the safe side, just in case it ever takes more than 1000 ms for the popup to appear.
  2. Improve performance, as it probably takes on average less than 1000 ms for the popup to appear.

If you're running a single-threaded application, then you might as well get rid of the Thread.sleep altogether, as there is nothing to gain by putting the only thread in the system to sleep. In addition to that, you may also want to consider giving up and exiting the loop after a certain number of attempts.

Burschenschaft answered 7/5, 2014 at 21:38 Comment(1)
That's an infinite loop if the alert is never displayed! Should enhance with a timeout.Leitao
A
1

I always try to avoid Thread.sleep() and unnecessary throw-catch exception.
Following solution will achieve both, you can change the count value as per your requirements. Also it wont cause a infinite loop if pop up didn't appear.

int count = 0;
do{
   if (driver.getWindowHandles().size() > 1) {
     // Alert is present
     driver.switchTo().alert().accept();
     break;
   }
} while(count < 100);
A answered 8/5, 2014 at 5:26 Comment(0)
C
0

The exception "NoSuchAlertError: no alert open", simply means your selenium cant see the alert box, this is normal because usually the alert isn't really part of the html element it is opened in another frame so you need to specify that you are traversing to another frame also you need to understand that there may be synchronization issues so that's why you need to specify a wait time until some conditions are met by doing this, so try this code below:

WebDriverWait waiting = new WebDriverWait(driver, Duration.ofSeconds(5));
waiting.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();

With WebDriverWait waiting = new WebDriverWait(driver, Duration.ofSeconds(5)); you are using an Explicit wait to ensure the the driver waits for 5 sec. With waiting.until(ExpectedConditions.alertIsPresent()); you are saying wait until an alert is present. With driver.switchTo().alert().accept(); you are switching to to the alert box and carrying out the accept function (dismiss() if you want to reject or cancel)

Cordova answered 3/5, 2023 at 18:25 Comment(1)
Hi, please use code formatting. Here is help page : formatting helpSoutheastwards

© 2022 - 2024 — McMap. All rights reserved.