diff --git a/src/SeleniumLibrary/locators/windowmanager.py b/src/SeleniumLibrary/locators/windowmanager.py index 8244c8236..a4bc76723 100644 --- a/src/SeleniumLibrary/locators/windowmanager.py +++ b/src/SeleniumLibrary/locators/windowmanager.py @@ -199,13 +199,11 @@ def _select_matching(self, matcher, error): def _get_current_window_info(self): try: - window_id, name = self.driver.execute_script( - "return [ window.id, window.name ];" - ) + identity = self.driver.execute_script("return [ window.id, window.name ];") except WebDriverException: - # The webdriver implementation doesn't support Javascript so we - # can't get window id or name this way. - window_id = name = None + identity = None + # Firefox may also return null if navigation interrupts the script. + window_id, name = identity if identity is not None else (None, None) return WindowInfo( self.driver.current_window_handle, window_id if window_id is not None else "undefined", diff --git a/utest/test/locators/test_windowmanager.py b/utest/test/locators/test_windowmanager.py index 393faad60..8bc6c00f3 100644 --- a/utest/test/locators/test_windowmanager.py +++ b/utest/test/locators/test_windowmanager.py @@ -82,6 +82,17 @@ def test_select_by_url(self): manager.select("url=http://localhost/page2.html") assert manager.driver.current_window.name == "win2" + def test_select_by_url_when_window_identity_script_returns_none(self): + manager = WindowManagerWithMockBrowser( + {"name": "win1", "title": "Title 1", "url": "http://localhost/page1.html"}, + {"name": "win2", "title": "Title 2", "url": "http://localhost/page2.html"}, + ) + manager.driver.execute_script = lambda script: None + + manager.select("url=http://localhost/page2.html") + + assert manager.driver.current_window.name == "win2" + def test_select_by_url_with_multiple_matches(self): manager = WindowManagerWithMockBrowser( {"name": "win1", "title": "Title 1", "url": "http://localhost/page1.html"},