2026-08-26 10:26:59 -07:00
|
|
|
import logging
|
2026-08-20 10:43:07 -04:00
|
|
|
from selenium.common.exceptions import WebDriverException, JavascriptException
|
2026-08-19 14:03:22 -04:00
|
|
|
from selenium import webdriver
|
|
|
|
|
|
2026-08-26 10:26:59 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-08-21 10:04:40 -04:00
|
|
|
GHOST_TAB_URLS = (
|
|
|
|
|
"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", # has fre
|
|
|
|
|
"https://ntp.msn.com/edge/ntp?locale=en-US&title=New%20tab&dsp=1&sp=Bing&feed_dis=always&en_widget_reg=false&prerender=1&PC=U531" # no fre
|
|
|
|
|
)
|
2026-08-19 14:03:22 -04:00
|
|
|
|
|
|
|
|
class TabUtils:
|
|
|
|
|
def __init__(self, driver: webdriver.Edge):
|
|
|
|
|
self.driver = driver
|
|
|
|
|
self.problematic_tabs = set()
|
|
|
|
|
|
2026-08-20 10:35:35 -04:00
|
|
|
def ensure_focus(self):
|
2026-08-20 10:43:07 -04:00
|
|
|
try:
|
|
|
|
|
self.driver.execute_script("""
|
2026-08-20 10:35:35 -04:00
|
|
|
Object.defineProperty(document, 'hidden', { get: () => false });
|
|
|
|
|
Object.defineProperty(document, 'visibilityState', { get: () => 'visible' });
|
|
|
|
|
Document.prototype.hasFocus = function() { return true; };
|
|
|
|
|
window.hasFocus = function() { return true; };
|
|
|
|
|
document.dispatchEvent(new Event('visibilitychange'));
|
|
|
|
|
""")
|
2026-08-20 10:43:07 -04:00
|
|
|
except JavascriptException: pass # it's probably a property redef exc
|
2026-08-20 10:35:35 -04:00
|
|
|
|
2026-08-19 14:03:22 -04:00
|
|
|
def switch_to_other_tab(self):
|
|
|
|
|
current_window = self.driver.current_window_handle
|
|
|
|
|
|
|
|
|
|
for handle in self.driver.window_handles:
|
|
|
|
|
if handle != current_window and handle not in self.problematic_tabs:
|
|
|
|
|
self.driver.switch_to.window(handle)
|
2026-08-20 09:28:54 -04:00
|
|
|
|
2026-08-21 10:04:40 -04:00
|
|
|
if self.driver.current_url in GHOST_TAB_URLS:
|
2026-08-26 10:26:59 -07:00
|
|
|
logger.info("Found ghost tab with handle %s and URL %s.", handle, self.driver.current_url)
|
2026-08-21 10:04:40 -04:00
|
|
|
continue
|
2026-08-20 10:35:35 -04:00
|
|
|
|
|
|
|
|
self.ensure_focus()
|
2026-08-19 14:03:22 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def close_all_other_tabs(self, exceptions: list[str] = None):
|
|
|
|
|
if exceptions is None:
|
|
|
|
|
exceptions = [self.driver.current_window_handle]
|
|
|
|
|
|
|
|
|
|
switch_back_to = exceptions[0]
|
|
|
|
|
|
|
|
|
|
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)
|
2026-08-20 09:28:54 -04:00
|
|
|
|
2026-08-21 10:04:40 -04:00
|
|
|
if self.driver.current_url in GHOST_TAB_URLS:
|
2026-08-26 10:26:59 -07:00
|
|
|
logger.info("Found ghost tab with handle %s and URL %s, not closing.", handle, self.driver.current_url)
|
2026-08-21 10:04:40 -04:00
|
|
|
continue
|
2026-08-20 11:19:35 -04:00
|
|
|
|
2026-08-21 10:04:40 -04:00
|
|
|
tab_url = self.driver.current_url
|
2026-08-20 09:28:54 -04:00
|
|
|
|
2026-08-20 11:19:35 -04:00
|
|
|
try:
|
|
|
|
|
self.driver.close()
|
2026-08-26 10:26:59 -07:00
|
|
|
logger.info("Closed tab with handle %s and URL %s.", handle, tab_url)
|
2026-08-20 11:19:35 -04:00
|
|
|
|
2026-08-19 14:03:22 -04:00
|
|
|
except WebDriverException:
|
2026-08-26 10:26:59 -07:00
|
|
|
logger.warning("Could not close tab with handle %s and URL %s.", handle, tab_url)
|
2026-08-19 14:03:22 -04:00
|
|
|
self.problematic_tabs.add(handle)
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
self.driver.switch_to.window(switch_back_to)
|