java Selenium WebDriver - 意外模式对话框警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30771067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:37:53  来源:igfitidea点击:

Selenium WebDriver - Unexpected modal dialog Alert

javaseleniumselenium-webdriverwebdriver

提问by sumon c

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:

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

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.

您需要使用 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" );
    }

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

在我的 WebDriver 代码中,我在 Driver 中使用了以下功能(正如这里的其他一些帖子所建议的那样)

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:

然后我进行了一个简单的 get 调用,包含在一个 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:

如果我不在驱动程序对象上编写任何方法并关闭驱动程序。程序在 Eclipse 中正常终止,但模态警报保持打开状态,尽管:

UnexpectedAlertBehaviour.ACCEPT

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

但是,如果我使用任何与驱动程序相关的方法或操作,就像 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.

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

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)

回答by Vikas Ojha

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

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.
  1. 将您的第二个操作包含在 try/catch 中并处理UnhandledAlertException
  2. 在那个 catch 块中,执行,驱动程序 -> 切换到 -> 警报 -> 接受
  3. 在catch块之后,再次执行第二个操作。