diff --git a/testing/web-platform/tests/webdriver/conftest.py b/testing/web-platform/tests/webdriver/conftest.py
index d614cd1be48a..13092678918c 100644
--- a/testing/web-platform/tests/webdriver/conftest.py
+++ b/testing/web-platform/tests/webdriver/conftest.py
@@ -36,6 +36,7 @@ def session(_session, request):
request.addfinalizer(lambda: cleanup.switch_to_top_level_browsing_context(_session))
request.addfinalizer(lambda: cleanup.restore_windows(_session))
request.addfinalizer(lambda: cleanup.dismiss_user_prompts(_session))
+ request.addfinalizer(lambda: cleanup.ensure_valid_window(_session))
return _session
diff --git a/testing/web-platform/tests/webdriver/util/cleanup.py b/testing/web-platform/tests/webdriver/util/cleanup.py
index c86d172550fa..7f3232f54816 100644
--- a/testing/web-platform/tests/webdriver/util/cleanup.py
+++ b/testing/web-platform/tests/webdriver/util/cleanup.py
@@ -1,5 +1,14 @@
import webdriver
+
+def ensure_valid_window(session):
+ """If current window is not open anymore, ensure to have a valid one selected."""
+ try:
+ session.window_handle
+ except webdriver.NoSuchWindowException:
+ session.window_handle = session.handles[0]
+
+
def dismiss_user_prompts(session):
"""Dismisses any open user prompts in windows."""
current_window = session.window_handle
@@ -13,6 +22,7 @@ def dismiss_user_prompts(session):
session.window_handle = current_window
+
def restore_windows(session):
"""Closes superfluous windows opened by the test without ending
the session implicitly by closing the last window.
@@ -26,6 +36,7 @@ def restore_windows(session):
session.window_handle = current_window
+
def switch_to_top_level_browsing_context(session):
"""If the current browsing context selected by WebDriver is a
`` or an `