Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium "NoSuchAlertError: no alert open" error

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)
like image 577
Keo Strife Avatar asked May 07 '14 20:05

Keo Strife


People also ask

How does selenium handle no such alert exception?

Hence NoAlertPresentException WebDriver Exception will be occurred, when the driver we mentioned in the Selenium Program code is unable to find the Alert Pop-up on the web page and in order to handle this, we have to use NoAletPresentException WebDriver Class in the catch block.

What is alert () in driver switchTo ()?

An alert can be of three types – a prompt which allows the user to input text, a normal alert and a confirmation alert. By default, the webdriver can only access the main page, once an alert comes up, the method switchTo(). alert() is used to shift the focus webdriver control to the alert.


2 Answers

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.

like image 65
Mrchief Avatar answered Sep 21 '22 23:09

Mrchief


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();
like image 45
Alpha Avatar answered Sep 17 '22 23:09

Alpha