Selenium WebDriver - 意外的模态对话框警报

sum*_*n c 5 java selenium webdriver selenium-webdriver

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

您需要使用IE 6.0来查看此应用程序.否则一些功能可能无法正常工作我检查了页面的来源,它有一个功能.模态警报不是HTML元素,我尝试使用FireBug查找任何元素,但无济于事.

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" );
    }
Run Code Online (Sandbox Code Playgroud)

在我的WebDriver代码中,我在驱动程序中使用了以下功能(如其他帖子所示)

DesiredCapabilities dc=new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);
WebDriver driver =new FirefoxDriver(dc);
Run Code Online (Sandbox Code Playgroud)

然后我正在进行一个简单的get调用,包含在try-catch中:

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

如果我不在驱动程序对象上写任何方法并关闭驱动程序.该程序通常在Eclipse中终止,但Modal Alert保持打开状态,尽管:

UnexpectedAlertBehaviour.ACCEPT
Run Code Online (Sandbox Code Playgroud)

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

String title = driver.getTitle();
Run Code Online (Sandbox Code Playgroud)

Java代码因异常而失败,但模式警报弹出窗口关闭!并且错误的最后一个行号作为我使用第一个驱动程序相关操作的行给出.

请分享你的想法......

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)
Run Code Online (Sandbox Code Playgroud)

Vik*_*jha 7

该行为是有意的.下面是它的工作原理 -

  1. 你发出driver.get(B).它触发浏览器打开网页,然后它与浏览器没有任何关系,因此它不关心是否打开警报.
  2. 页面加载时,会出现弹出对话框,但代码端或Eclipse没有任何反应.
  3. 当您尝试执行另一个操作时,它会与浏览器交互并看到意外的弹出对话框.

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

  1. 将第二个操作包含在try/catch中并处理UnhandledAlertException
  2. 在catch块内,执行,driver - >切换到 - > alert - > accept
  3. 在catch块之后,再次执行第二个操作.

  • 感谢您的回答。我也对异常发生的时间感到困惑。 (2认同)