简体   繁体   中英

Selenium WebDriver - Unexpected modal dialog Alert

I am trying to use WebDriver to automate an website. I am using Firefox Driver, but the homepage has a Pop-up modal alert window: saying:

You need to use IE 6.0 for viewing this application. Else some features may not work I checked the Source of the page, it has a function. The Modal Alert is not an HTML element, I tried finding any element with FireBug, but to no avail.

if ( strBrowName == "Microsoft Internet Explorer" )
{
    if ( (( strBrowVersion.indexOf( 'MSIE 6' ) ) > 0 ) ) 
    {       
    }
    else
    {
        alert( "You need to use IE 6.0 for viewing this application. Else some features may not work" );
    }

In my WebDriver code I am using the following capability in the Driver (as suggested by some other post here)

DesiredCapabilities dc=new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver =new FirefoxDriver(dc);

Then I am making a simple get call, enclosed in a try-catch:

try {
                 driver.get(B);
             }
             catch (UnhandledAlertException e) {
                 System.err.println("Caught UnhandledAlertException: ");                 
             }
             System.out.println("URL Opened");

If I do not write any method on the driver object and close the driver instead. The program terminates in Eclipse normally, but the Modal Alert stays open, inspite of the:

UnexpectedAlertBehaviour.ACCEPT

But, if I use ANY driver related method or operation, like, as simple as getTitle:

String title = driver.getTitle();

The Java code fails with Exception, BUT the modal Alert pop-up closes! And the last line number of the error is given as the line where I used the first driver related operation.

Please share your thoughts...

Exception in thread "main" org.openqa.selenium.UnhandledAlertException: Unexpected modal dialog (text: You need to use IE 6.0 for viewing this application. Else some features may not work): You need to use IE 6.0 for viewing this application. Else some features may not work
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'LFY2DSY1', ip: '30.142.106.199', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=38.0.5, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: a97ab146-4929-4502-98f2-810169cc5532
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:185)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:152)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:628)
    at org.openqa.selenium.remote.RemoteWebDriver.getTitle(RemoteWebDriver.java:319)
    at SelPkg.CIRS.main(CIRS.java:76)

The behaviour is intended. Here is how it works -

  1. You issue driver.get(B). It triggers the browser to open the webpage and then it doesnt have anything to do with the browser, so it doesnt care whether an alert is open or not.
  2. When the page loads, pop-up dialog appears, but nothing happens on your code side or Eclipse.
  3. When you try to perform another operation, it interacts with browser and sees an unexpected popup dialog.

Now, the problem occurs that modal dialog closes and still exception occurs, so try the following.

  1. Enclose your second operation in a try/catch and handle UnhandledAlertException
  2. Inside that catch block, perform, driver -> switch to -> alert -> accept
  3. After the catch block, perform the second operation again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM