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

标签 java selenium selenium-webdriver

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

  1. 打开浏览器 --> 导航到并单击打开新窗口的链接
  2. 使用driver.switchTo().window(handle)切换到子窗口 --> 在子窗口上执行操作。
  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 步周围的异常 block 打印了错误!!

这是我的问题: 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());

最佳答案

你是对的,使用 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

关于java - driver.close() 关闭子窗口后切换到父窗口抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35294434/

相关文章:

java - VisualVM - "Used heap"中包含什么?

angularjs - 在 Protractor 2.0 中,在 beforeAll 中调用 clear() 行为不当

selenium - TestNG中BeforeClass和BeforeTest的区别

java - 如何在 javafx 中将图像裁剪成碎片,以便用图 block 创建游戏拼图?

java - 使用 SVN 正确将 Apache nutch 导入 eclipse EE juno

java - 试图完全理解 For 循环

python - Selenium 错误(截屏时) 'NoneType' 对象没有属性 'encode'

selenium - 如何选择这个文本区域?

python - selenium.common.exceptions.WebDriverException : Message: connection refused

python - 我们如何等待消息文本在 selenium 中弹出