在子窗口被driver.close()关闭后切换到父窗口抛出异常

时间:2016-02-09 14:22:34

标签: java selenium selenium-webdriver flash-selenium

我有一个场景,我执行以下步骤:

  1. 打开浏览器 - >导航到并单击打开新窗口的链接
  2. 使用driver.switchTo().window(handle)切换到子窗口 - >在子窗口上执行操作。
  3. 关闭子窗口。
  4. 切换回父窗口。
  5. 获取当前窗口的标题。
  6. 步骤1到3工作正常。但是第4步抛出异常。为了更清楚,我在throw.n

    之前包含了日志消息
    // Switching to child window
    [testng] 23:40:33.794 INFO - Executing: [switch to window: 3efe9598-2a08-40ec-a950-d4a6e2182b20])
    [testng] W 2016-02-08 23:40:33:808 Browser.cpp(241) Unable to get window name, IHTMLWindow2::get_name failed or returned a NULL value
    [testng] W 2016-02-08 23:40:33:821 Browser.cpp(241) Unable to get window name, IHTMLWindow2::get_name failed or returned a NULL value
    [testng] 23:40:33.942 INFO - Done: [switch to window: 3efe9598-2a08-40ec-a950-d4a6e2182b20]
    [testng] 23:40:33.944 INFO - Executing: [get title])
    [testng] 23:40:33.955 INFO - Done: [get title]
    [testng] 23:41:34.708 INFO - Executing: [close window])
    [testng] 23:41:34.719 INFO - Done: [close window]
    // Switch to parent window
    [testng] 23:41:34.751 INFO - Executing: [switch to window: b2dd5b6e-c891-498f-871b-1fc80b4afef2])
    [testng] W 2016-02-08 23:41:34:753 IECommandExecutor.cpp(595) Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
    [testng] W 2016-02-08 23:41:34:753 IECommandExecutor.cpp(504) Unable to find current browser
    [testng] W 2016-02-08 23:41:34:765 Browser.cpp(241) Unable to get window name, IHTMLWindow2::get_name failed or returned a NULL value
    [testng] W 2016-02-08 23:41:34:865 IECommandExecutor.cpp(595) Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
    [testng] W 2016-02-08 23:42:34:875 response.cc(69) Error response has status code 21 and message 'Timed out waiting for page to load.' message
    [testng] 23:42:34.879 WARN - Exception thrown
    [testng] org.openqa.selenium.TimeoutException: Timed out waiting for page to load. (WARNING: The server did not provide any stacktrace information)
    [testng] Command duration or timeout: 60.13 seconds
    [testng] Build info: version: '2.43.1', revision: '5163bceef1bc36d43f3dc0b83c88998168a363a0', time: '2014-09-10 09:43:55'
    [testng] System info: host: 'x-x-x-x', ip: 'x.x.x.x', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
    [testng] Driver info: org.openqa.selenium.ie.InternetExplorerDriver
    [testng] Capabilities [{browserAttachTimeout=0, enablePersistentHover=false, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:42778/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=true, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=accept}]`
    

    我们正在使用基于flashSelenium的特殊框架,我们的开发人员已经设法将Flash Selenium与WebDriver集成(最初闪存Selenium旨在与RC一起使用)。根据我的理解,我们的框架涉及flashSelenium,RC和Webdriver组件。我是团队的新手,框架API没有处于活动开发状态(不支持)。

    当我在第4步尝试捕捉时,第5步正确执行。我可以看到步骤4周围的异常块打印错误!!

    所以这是我的问题: 在任何情况下,driver.switchTo()。window(parentWindow)都会给出如上所示的异常吗?

    从下面的日志段开始,我可以看到切换到窗口但是切换窗口的内部方法会抛出异常,因为连续的行表示。

    [testng] 23:41:34.751 INFO - Executing: [switch to window: b2dd5b6e-c891-498f-871b-1fc80b4afef2])
    [testng] W 2016-02-08 23:41:34:753 IECommandExecutor.cpp(595) Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
    [testng] W 2016-02-08 23:41:34:753 IECommandExecutor.cpp(504) Unable to find current browser
    [testng] W 2016-02-08 23:41:34:765 Browser.cpp(241) Unable to get window name, IHTMLWindow2::get_name failed or returned a NULL value
    [testng] W 2016-02-08 23:41:34:865 IECommandExecutor.cpp(595) Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
    [testng] W 2016-02-08 23:42:34:875 response.cc(69) Error response has status code 21 and message 'Timed out waiting for page to load.' message
    [testng] 23:42:34.879 WARN - Exception thrown
    

    代码如下所示

    String parentHandle = driver.getWindowHandle();
    // Click on the element code goes here (This is in flash selenium)
    Set<String> windowhandles = driver.getWindowHandles(); 
    for (String windowHandle : windowHandles) {
        driver.switchTo().window(windowHandle);
    }
    
    System.out.println("Child title : " + driver.getTitle());
    driver.close;
    // Exception is thrown in the below line. If a try catch is placed
    // the last line gets executed (sysout). But I can see the catch getting executed.
    driver.switchTo().window(parentHandle);
    System.out.println("Child title : " + driver.getTitle());
    

1 个答案:

答案 0 :(得分:1)

你是对的,使用driver.close()后,Selenium不会自动切换到父窗口。当您尝试driver.switchTo().window(currentWindow)时,驱动程序仍然专注于关闭窗口的窗口句柄,这导致错误日志中出现Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20Unable to find current browser消息。

如果要切换回父句柄,则应在切换之前保存它

String parentHandle = driver.getWindowHandle();
driver.switchTo().window(childHandle); // switch to child window
driver.close(); // close child window
driver.switchTo().window(parentHandle); // switch focus back to parent window