add exception handling for possible redefinition of document focus handlers

This commit is contained in:
Carl Furtado
2026-08-20 10:43:07 -04:00
parent a089b1f209
commit af2eb68940
2 changed files with 5 additions and 3 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ keyboard = mimic_typing.KeyboardUtils(driver)
rewards = rewards_tasks.RewardsTaskUtils(driver)
rewards.complete_required_searches()
rewards.complete_all_tasks()
input("Press Enter to exit...")
+3 -1
View File
@@ -1,4 +1,4 @@
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import WebDriverException, JavascriptException
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"
@@ -9,6 +9,7 @@ class TabUtils:
self.problematic_tabs = set()
def ensure_focus(self):
try:
self.driver.execute_script("""
Object.defineProperty(document, 'hidden', { get: () => false });
Object.defineProperty(document, 'visibilityState', { get: () => 'visible' });
@@ -16,6 +17,7 @@ Document.prototype.hasFocus = function() { return true; };
window.hasFocus = function() { return true; };
document.dispatchEvent(new Event('visibilitychange'));
""")
except JavascriptException: pass # it's probably a property redef exc
def switch_to_other_tab(self):
current_window = self.driver.current_window_handle