5

In selenium framework 2.25, I see that we have the UnexpectedAlertBehaviour enum type, but I don't know how to use it.

1

5 Answers 5

11

I found this portion of documentation on your issue: This may be useful for other people as well:

v2.25.0

=======

WebDriver:

  • Added API for dealing with BASIC and DIGEST authentication

    dialogs. Currently not implemented in any drivers.

  • Warn users that the IE driver will no longer use the DLL in the

    next release.

  • Deprecated browser specific WebElement subclasses.

  • Added support for "requiredCapabilities" to the remote webdrivers

    and implemented basic support for these in the firefox

    driver. Failure to fulfull a required capability will cause a

    SessionNotCreatedException to be thrown.

  • Added the ability to determine how unhandled alerts should be handled. This is handled by the "unexpectedAlertBehaviour" capability, which can be one of "accept", "dismiss" or "ignore". Java code should use the UnexpectedAlertBehaviour enum. This is only implemented in Firefox for now.

  • Allow native events to be configured in Firefox and

    (experimentally) in IE using the "nativeEvents" capability.

  • Updated supported versions of Firefox to 17.

.....

Whole list provided here

An here is the source

package org.openqa.selenium;

    public enum UnexpectedAlertBehaviour {

      ACCEPT ("accept"),
      DISMISS ("dismiss"),
      IGNORE ("ignore")
      ;

      private String text;

      private UnexpectedAlertBehaviour(String text) {
        this.text = text;
      }

      @Override
      public String toString() {
        return String.valueOf(text);
      }

      public static UnexpectedAlertBehaviour fromString(String text) {
        if (text != null) {
          for (UnexpectedAlertBehaviour b : UnexpectedAlertBehaviour.values()) {
            if (text.equalsIgnoreCase(b.text)) {
              return b;
            }
          }
        }
        return null;
      }
    }

As I see you use unexpectedAlertBehaviour to decide whether alert is unhandled and if it is so, you'll decide how to handle it.

I suppose it should be something like (my assumption):

try{
alert.accept();
}

catch(org.openqa.selenium.UnexpectedAlertBehaviour){
///...blablabla
}
3
  • 3
    I don't think so. In fact, the UnexpectedAlertBehaviour isn't an exception which is thrown by alert.accept(). Oct 17, 2012 at 2:10
  • This is an enum and not an exception to be thrown or caught. Jan 2, 2013 at 12:15
  • Nice @xyz, i see your "words" logo. You have also a sand clock on it :D
    – m3nda
    Jun 9, 2015 at 12:39
3

It is a CapabilityType, so you have to express it in the DesiredCapabilities that you pass when creating the driver. In Python I added this behavior to my FireFox drivers using this code:

        selenium.webdriver.DesiredCapabilities.FIREFOX["unexpectedAlertBehaviour"] = "accept"

I haven't tested this Java but in theory it should work:

DesiredCapabilities cap = new DesiredCapabailities();
cap.setPreference(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, 
   org.openqa.selenium.UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver = new FirefoxDriver(cap);
3
  • In Python, do I need to import something first? Aug 4, 2021 at 2:14
  • I found that it should be this: from selenium.webdriver.common.desired_capabilities import DesiredCapabilities Aug 4, 2021 at 2:26
  • What is this supposed to do? Does it mean that all alerts will be automatically clicked "accept" by the driver? That would be great, but somehow I can't get it working... Aug 5, 2021 at 10:10
2

To manage any of the errors you can got, you only have to put into a try/block using the Exception NAMEERROR like this:

from selenium.common.exceptions import UnexpectedAlertBehaviour
from selenium.common.exceptions import UnexpectedAlertPresentException
try:
  var = driver.some_function()
  return True
except UnexpectedAlertBehaviour:
  print "We have raised the UnexpectedAlertBehaviour"
  return False
except UnexpectedAlertPresentException:
  print "UnexpectedAlertPresentException"
  return False

I know this code is not in Java, but the basis is the same. Try/Catch with name of exception. You can see an example of this on my post of the alerts() handling here

1

If you are using Selenium with NodeJS then you can use options.setAlertBehavior(UserPromptHandler.ACCEPT).

Full example (Typescript):


import { Builder } from 'selenium-webdriver';
import { Options } from 'selenium-webdriver/chrome';
import { UserPromptHandler } from 'selenium-webdriver/lib/capabilities';

(async () => {
    let options = new Options()
    options.addArguments(`--user-data-dir=./profile`)
    options.addArguments(`--no-sandbox`)
    options.windowSize({ width: 1280, height: 720 })
    options.setAlertBehavior(UserPromptHandler.ACCEPT)
    let driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
})()

You don't need to look for anything related to DesiredCapabilities or Capabilities, you can fill that value with options variable.

0

Try to do like this:

DesiredCapabilities ff = DesiredCapabilities.firefox();
ff.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR  UnexpectedAlertBehaviour.ACCEPT);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.