|
18 | 18 | from .command import Command
|
19 | 19 | from selenium.webdriver.common.alert import Alert
|
20 | 20 | from selenium.webdriver.common.by import By
|
21 |
| -from selenium.common.exceptions import NoSuchElementException, NoSuchFrameException |
| 21 | +from selenium.common.exceptions import NoSuchElementException, NoSuchFrameException, NoSuchWindowException |
22 | 22 |
|
23 | 23 | try:
|
24 | 24 | basestring
|
@@ -106,7 +106,27 @@ def window(self, window_name):
|
106 | 106 | :Usage:
|
107 | 107 | driver.switch_to.window('main')
|
108 | 108 | """
|
109 |
| - data = {'name': window_name} |
110 | 109 | if self._driver.w3c:
|
111 |
| - data = {'handle': window_name} |
| 110 | + self._w3c_window(window_name) |
| 111 | + return |
| 112 | + data = {'name': window_name} |
112 | 113 | self._driver.execute(Command.SWITCH_TO_WINDOW, data)
|
| 114 | + |
| 115 | + def _w3c_window(self, window_name): |
| 116 | + def send_handle(h): |
| 117 | + self._driver.execute(Command.SWITCH_TO_WINDOW, {'handle': h}) |
| 118 | + |
| 119 | + try: |
| 120 | + # Try using it as a handle first. |
| 121 | + send_handle(window_name) |
| 122 | + except NoSuchWindowException as e: |
| 123 | + # Check every window to try to find the given window name. |
| 124 | + original_handle = self._driver.current_window_handle |
| 125 | + handles = self._driver.window_handles |
| 126 | + for handle in handles: |
| 127 | + send_handle(handle) |
| 128 | + current_name = self._driver.execute_script('return window.name') |
| 129 | + if window_name == current_name: |
| 130 | + return |
| 131 | + send_handle(original_handle) |
| 132 | + raise e |
0 commit comments