当前位置: 首页 > 知识库问答 >
问题:

在驱动程序关闭子窗口后切换到父窗口。close()引发异常

伊俊能
2023-03-14

我在一个场景中执行以下步骤:

  1. 打开浏览器-->导航到并单击打开新窗口的链接
  2. 使用driver.switchTo()切换到子窗口。window(句柄)-->在子窗口上执行操作。
  3. 关闭子窗口。
  4. 切换回父窗口。
  5. 获取当前窗口的标题。

步骤1到3工作很好。但是步骤4抛出了一个异常。为了更清楚,我在抛出异常之前包含了日志消息。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集成在一起(最初flash Selenium是用来与RC一起工作的)。根据我的理解,我们的框架涉及flashSelenium、RC和Webdriver组件。我是这个团队的新手,框架API还没有在积极开发中(没有支持)。

当我在步骤4周围放置try catch时,步骤5将正确执行。我可以看到异常阻止了步骤4打印错误!!

所以我的问题来了:driver.switchTo().window(parentWindow)在任何情况下是否会给出如上所示的异常?

从下面的日志片段中,我可以看到到window的切换执行了,但是switch 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

代码如下所示

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个答案

阳德润
2023-03-14

您是正确的,Selenium在使用driver.close()后不会自动切换到父窗口。当您尝试driver.switchTo().window(currentWindow)时,驱动程序仍然专注于关闭窗口的窗口句柄,这是导致无法找到id为3EFE9598-2A08-40EC-A950-D4A6E2182B20的托管浏览器和无法找到当前浏览器消息的原因。

如果您想切换回父句柄,您应该在切换之前保存它

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
 类似资料: