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

标签 java selenium selenium-webdriver webdriver

我正在尝试使用 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.

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 代码中,我在驱动程序中使用了以下功能(正如此处其他帖子所建议的那样)

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

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

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

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

UnexpectedAlertBehaviour.ACCEPT

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

String title = driver.getTitle();

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)

最佳答案

行为是有意的。这是它的工作原理 -

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

现在出现模态对话框关闭的问题,仍然出现异常,请尝试以下操作。

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

关于java - Selenium WebDriver - 意外的模态对话框警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30771067/

相关文章:

java - java中的嵌套方法?

java - SimpleDateFormat 中日期解析的错误行为

url - 如何在 Selenium IDE 中从 'link' 元素获取 url

java - 无法替换表列中的值

java - 集线器已关闭或没有响应 : Unexpected char 0x131 at 23 in User-Agent value: selenium/3. 14.0 (java w²ndows) SeleniumGrid v3.14.0

java - 什么时候(真的)需要不可修改的 map ?

java - 返回按钮 Android WebView

javascript - 使用 Selenium C# 将 Chrome 缩放设置为 100%

javascript - 永久/每次刷新时注入(inject) javascript 和 selenium

java - Selenium:元素未通过 LINK_TEXT 或 PARTIAL_LINK_TEXT 定位