Fosowl/agenticSeek · error · Exception

Failed to initialize browser: {str(e)}

Error message

Failed to initialize browser: {str(e)}

What it means

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.

Source

Thrown at sources/browser.py:298

            fix_hairline=True,
        )
        return driver
    else:
        return webdriver.Chrome(service=service, options=chrome_options)

class Browser:
    def __init__(self, driver, anticaptcha_manual_install=False):
        """Initialize the browser with optional AntiCaptcha installation."""
        self.js_scripts_folder = "./sources/web_scripts/" if not __name__ == "__main__" else "./web_scripts/"
        self.anticaptcha = "https://chrome.google.com/webstore/detail/nopecha-captcha-solver/dknlfmjaanfblgfdfebhijalfmhmjjjo/related"
        self.logger = Logger("browser.log")
        self.screenshot_folder = runtime_subdir("screenshots")
        self.tabs = []
        try:
            self.driver = driver
            self.wait = WebDriverWait(self.driver, 10)
        except Exception as e:
            raise Exception(f"Failed to initialize browser: {str(e)}")
        self.setup_tabs()
        self.patch_browser_fingerprint()
        if anticaptcha_manual_install:
            self.load_anticatpcha_manually()
    
    def setup_tabs(self):
        self.tabs = self.driver.window_handles
        try:
            self.driver.get("https://www.google.com")
        except Exception as e:
            self.logger.log(f"Failed to setup initial tab:" + str(e))
            pass
        self.screenshot()
    
    def switch_control_tab(self):
        self.logger.log("Switching to control tab.")
        self.driver.switch_to.window(self.tabs[0])
            

View on GitHub (pinned to ae57a23577)

Solutions

  1. Create a fresh WebDriver instance and pass it to Browser instead of a previously quit/reused one.
  2. Read the chained message in str(e) — it contains the underlying Selenium error (e.g. 'invalid session id'); fix that root cause.
  3. Verify browser and driver versions match (e.g. chromedriver vs installed Chrome) and that the driver starts standalone.
  4. Ensure you pass a real selenium WebDriver instance (not None or a closed driver) to Browser(driver=...).

Example fix

// before
driver = webdriver.Chrome()
driver.quit()  # session now dead
browser = Browser(driver=driver)  # Failed to initialize browser
// after
driver = webdriver.Chrome()
browser = Browser(driver=driver)  # fresh, live session
Defensive patterns

Strategy: try-catch

Validate before calling

from selenium.webdriver.remote.webdriver import WebDriver
if not isinstance(driver, WebDriver):
    raise TypeError("Browser() requires a live selenium WebDriver")
_ = driver.title  # probe session; raises Selenium 'invalid session id' early if dead

Type guard

def has_live_session(d) -> bool:
    try:
        _ = d.current_url
        return True
    except Exception:
        return False

Try / catch

try:
    browser = Browser(driver=driver)
except Exception as e:
    logger.error(f"browser init failed: {e}")
    driver = webdriver.Chrome()  # rebuild a fresh driver
    browser = Browser(driver=driver)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Fosowl/agenticSeek@ae57a23577 (2026-08-30). Data as JSON: /api/errors/37488c7e1df42aec. Report an issue: GitHub.