Files
rewards-farmer/src/tab_utils.py
T

33 lines
1018 B
Python
Raw Normal View History

2026-08-19 14:03:22 -04:00
from selenium.common.exceptions import WebDriverException
from selenium import webdriver
class TabUtils:
def __init__(self, driver: webdriver.Edge):
self.driver = driver
self.problematic_tabs = set()
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)
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)
try: self.driver.close()
except WebDriverException:
print(f"[WARNING] Could not close tab with handle {handle}.")
self.problematic_tabs.add(handle)
pass
self.driver.switch_to.window(switch_back_to)