Skip to content

Commit 2af7ffb

Browse files
committed
add fix for 'No Such Window' error
reason: from 6.4 secotion in http://www.w3.org/TR/webdriver/, it says: Once the window has closed, future commands must return a status code of no such window until a new window is selected for receiving commands. how to fix: 1. let close_window() automaticly switches to prevoius window; 2. capture NoSuchWindowException when select_window() / _select_matching()/get_current_window_handle()
1 parent ac83046 commit 2af7ffb

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Selenium2Library/keywords/_browsermanagement.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,15 @@ def switch_browser(self, index_or_alias):
212212
# Public, window management
213213

214214
def close_window(self):
215-
"""Closes currently opened pop-up window."""
215+
"""Closes currently opened pop-up window and switch back to previous window.
216+
It assumes the last opened window is in the last index of window list.
217+
However, as opened windows are list-returned from tree-order in remote end,
218+
please use 'Select Window' if automatic switching does not work as expected.
219+
"""
216220
self._current_browser().close()
221+
handles = self._current_browser().get_window_handles()
222+
if len(handles) >= 1:
223+
self._current_browser().switch_to_window(handles[-1])
217224

218225
def get_window_identifiers(self):
219226
"""Returns and logs id attributes of all windows known to the browser."""

src/Selenium2Library/locators/windowmanager.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,13 @@ def _get_window_infos(self, browser):
9595
return window_infos
9696

9797
def _select_matching(self, browser, matcher, error):
98-
starting_handle = browser.get_current_window_handle()
98+
try:
99+
starting_handle = browser.get_current_window_handle()
100+
except NoSuchWindowException: pass
99101
for handle in browser.get_window_handles():
100102
browser.switch_to_window(handle)
101103
if matcher(browser.get_current_window_info()):
102104
return
103-
browser.switch_to_window(starting_handle)
105+
if starting_handle:
106+
browser.switch_to_window(starting_handle)
104107
raise ValueError(error)

0 commit comments

Comments
 (0)