Selenium WebDriver-意外的模式对话框警报 [英] Selenium WebDriver - Unexpected modal dialog Alert

查看:145
本文介绍了Selenium WebDriver-意外的模式对话框警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WebDriver来自动化网站. 我正在使用Firefox驱动程序,但是主页上有一个弹出模式警报窗口:说:

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:

您需要使用IE 6.0来查看此应用程序.其他一些 功能可能不起作用 我检查了页面的来源,它有一个功能. Modal Alert不是HTML元素,我尝试使用FireBug查找任何元素,但无济于事.

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" );
    }

在我的WebDriver代码中,我在驱动程序中使用了以下功能(如此处其他文章所建议的那样)

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);

然后我在try-catch中进行了一个简单的get调用:

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");

如果我没有在驱动程序对象上编写任何方法,而是关闭驱动程序. 该程序通常会在Eclipse中终止,但是尽管存在以下情况,但Modal Alert仍保持打开状态:

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

但是,如果我使用任何与驱动程序相关的方法或操作,就像getTitle一样简单:

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

String title = driver.getTitle();

Java代码失败并显示Exception,但是模式Alert弹出窗口关闭! 错误的最后一行被指定为我使用的行 第一个与驱动程序相关的操作.

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. 您发出driver.get(B).它触发浏览器打开网页,然后与浏览器没有任何关系,因此它不在乎警报是否打开.
  2. 页面加载后,会弹出一个对话框,但是在代码端或Eclipse上什么也没有发生.
  3. 当您尝试执行其他操作时,它会与浏览器交互并看到意外的弹出对话框.

现在,出现模式对话框关闭且仍然发生异常的问题,因此请尝试以下操作.

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

  1. 在try/catch中包含第二个操作,并处理 UnhandledAlertException
  2. 在该catch块内,执行驱动程序->切换至->警报->接受
  3. 在catch块之后,再次执行第二个操作.

这篇关于Selenium WebDriver-意外的模式对话框警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆