add better handling for ghost tab

This commit is contained in:
Carl Furtado
2026-08-20 09:28:54 -04:00
parent dd50c850ac
commit 10bf499de1
3 changed files with 113 additions and 2 deletions
+6
View File
@@ -1,6 +1,7 @@
from selenium.common.exceptions import WebDriverException
from selenium import webdriver
GHOST_TAB_URL = "https://ntp.msn.com/edge/ntp?locale=en-US&title=New%20tab&fre=1&dsp=1&sp=Bing&feed_dis=always&en_widget_reg=false&prerender=1&PC=U531"
class TabUtils:
def __init__(self, driver: webdriver.Edge):
@@ -13,6 +14,8 @@ class TabUtils:
for handle in self.driver.window_handles:
if handle != current_window and handle not in self.problematic_tabs:
self.driver.switch_to.window(handle)
if self.driver.current_url == GHOST_TAB_URL: continue
return
def close_all_other_tabs(self, exceptions: list[str] = None):
@@ -24,6 +27,9 @@ class TabUtils:
for handle in self.driver.window_handles:
if handle not in exceptions and handle not in self.problematic_tabs:
self.driver.switch_to.window(handle)
if self.driver.current_url == GHOST_TAB_URL: continue
try: self.driver.close()
except WebDriverException:
print(f"[WARNING] Could not close tab with handle {handle}.")