{"record":{"id":"37488c7e1df42aec","repo":"Fosowl/agenticSeek","slug":"failed-to-initialize-browser-str-e","errorCode":null,"errorMessage":"Failed to initialize browser: {str(e)}","messagePattern":"Failed to initialize browser: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/browser.py","lineNumber":298,"sourceCode":"            fix_hairline=True,\n        )\n        return driver\n    else:\n        return webdriver.Chrome(service=service, options=chrome_options)\n\nclass Browser:\n    def __init__(self, driver, anticaptcha_manual_install=False):\n        \"\"\"Initialize the browser with optional AntiCaptcha installation.\"\"\"\n        self.js_scripts_folder = \"./sources/web_scripts/\" if not __name__ == \"__main__\" else \"./web_scripts/\"\n        self.anticaptcha = \"https://chrome.google.com/webstore/detail/nopecha-captcha-solver/dknlfmjaanfblgfdfebhijalfmhmjjjo/related\"\n        self.logger = Logger(\"browser.log\")\n        self.screenshot_folder = runtime_subdir(\"screenshots\")\n        self.tabs = []\n        try:\n            self.driver = driver\n            self.wait = WebDriverWait(self.driver, 10)\n        except Exception as e:\n            raise Exception(f\"Failed to initialize browser: {str(e)}\")\n        self.setup_tabs()\n        self.patch_browser_fingerprint()\n        if anticaptcha_manual_install:\n            self.load_anticatpcha_manually()\n    \n    def setup_tabs(self):\n        self.tabs = self.driver.window_handles\n        try:\n            self.driver.get(\"https://www.google.com\")\n        except Exception as e:\n            self.logger.log(f\"Failed to setup initial tab:\" + str(e))\n            pass\n        self.screenshot()\n    \n    def switch_control_tab(self):\n        self.logger.log(\"Switching to control tab.\")\n        self.driver.switch_to.window(self.tabs[0])\n            ","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/Fosowl/agenticSeek/blob/ae57a2357745a9706cb12d0fd76d954c84d166fa/sources/browser.py#L280-L316","documentation":"This generic Exception is raised in Browser.__init__ when assigning the Selenium driver or creating the WebDriverWait fails. The library wraps any exception from this step so that the root cause (e.g. an invalid/quit driver session) is surfaced with a clear 'browser failed to initialize' message via str(e). It means the Browser object could not be constructed and no tabs/fingerprint patching was performed.","triggerScenarios":"Passing a driver whose session is already dead (e.g. driver.quit() was called earlier), passing a non-Selenium object as `driver`, or WebDriver failing during WebDriverWait construction (WebDriver with a 10s timeout). Any exception in `self.driver` assignment or `WebDriverWait(self.driver, 10)` triggers it.","commonSituations":"Reusing a driver after the browser was closed; a ChromeDriver/browser version mismatch that killed the session; constructing Browser twice with the same driver; passing None or a mock that raises on attribute access.","solutions":["Create a fresh WebDriver instance and pass it to Browser instead of a previously quit/reused one.","Read the chained message in str(e) — it contains the underlying Selenium error (e.g. 'invalid session id'); fix that root cause.","Verify browser and driver versions match (e.g. chromedriver vs installed Chrome) and that the driver starts standalone.","Ensure you pass a real selenium WebDriver instance (not None or a closed driver) to Browser(driver=...)."],"exampleFix":"// before\ndriver = webdriver.Chrome()\ndriver.quit()  # session now dead\nbrowser = Browser(driver=driver)  # Failed to initialize browser\n// after\ndriver = webdriver.Chrome()\nbrowser = Browser(driver=driver)  # fresh, live session","handlingStrategy":"try-catch","validationCode":"from selenium.webdriver.remote.webdriver import WebDriver\nif not isinstance(driver, WebDriver):\n    raise TypeError(\"Browser() requires a live selenium WebDriver\")\n_ = driver.title  # probe session; raises Selenium 'invalid session id' early if dead","typeGuard":"def has_live_session(d) -> bool:\n    try:\n        _ = d.current_url\n        return True\n    except Exception:\n        return False","tryCatchPattern":"try:\n    browser = Browser(driver=driver)\nexcept Exception as e:\n    logger.error(f\"browser init failed: {e}\")\n    driver = webdriver.Chrome()  # rebuild a fresh driver\n    browser = Browser(driver=driver)","preventionTips":["Never reuse a driver after calling quit(); create a new one per Browser instance.","Keep browser and driver versions matched (webdriver-manager or matching chromedriver).","Probe driver.current_url before constructing Browser to catch dead sessions early.","Construct exactly one Browser per driver; avoid double-initialization."],"tags":["selenium","webdriver","browser-init","python"],"backgroundTag":"webdriver-invalid-session","analyzedSha":"ae57a2357745a9706cb12d0fd76d954c84d166fa","analyzedAt":"2026-08-30T02:49:05.834Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}