我有一个执行以下步骤的场景:
- 打开浏览器 -->导航到并单击链接,这将打开一个新窗口
- 使用
driver.switchTo().window(handle)
切换到子窗口 -->对子窗口执行操作。 - 关闭子窗口。
- 切换回父窗口。
- 获取当前窗口的标题。
步骤 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集成(最初Flash 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());
你是对的,Selenium在使用driver.close()
后不会自动切换到父窗口。当您尝试driver.switchTo().window(currentWindow)
时,驱动程序仍然专注于关闭窗口的窗口句柄,导致错误日志中Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
和Unable 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