From 5568a9ad3821fc037407956b7d977bc7445ee48d Mon Sep 17 00:00:00 2001 From: NaabZer Date: Thu, 7 May 2026 18:10:40 +0900 Subject: [PATCH] Change browser_adapter to use Stealth import instead of stealth_* --- crawl4ai/browser_adapter.py | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/crawl4ai/browser_adapter.py b/crawl4ai/browser_adapter.py index 3d3f5cdc4..2f4c15827 100644 --- a/crawl4ai/browser_adapter.py +++ b/crawl4ai/browser_adapter.py @@ -153,35 +153,27 @@ class StealthAdapter(BrowserAdapter): def __init__(self): self._console_script_injected = {} + self._stealth = None self._stealth_available = self._check_stealth_availability() def _check_stealth_availability(self) -> bool: - """Check if playwright_stealth is available and get the correct function""" + """Check if playwright_stealth is importable and instantiate the Stealth helper.""" try: - from playwright_stealth import stealth_async - self._stealth_function = stealth_async - return True + from playwright_stealth import Stealth except ImportError: - try: - from playwright_stealth import stealth_sync - self._stealth_function = stealth_sync - return True - except ImportError: - self._stealth_function = None - return False + return False + self._stealth = Stealth() + return True async def apply_stealth(self, page: Page): """Apply stealth to a page if available""" - if self._stealth_available and self._stealth_function: - try: - if hasattr(self._stealth_function, '__call__'): - if 'async' in getattr(self._stealth_function, '__name__', ''): - await self._stealth_function(page) - else: - self._stealth_function(page) - except Exception as e: - # Fail silently or log error depending on requirements - pass + if not (self._stealth_available and self._stealth): + return + try: + await self._stealth.apply_stealth_async(page) + except Exception: + # Fail silently or log error depending on requirements + pass async def evaluate(self, page: Page, expression: str, arg: Any = None) -> Any: """Standard Playwright evaluate with stealth applied""" @@ -418,4 +410,4 @@ def get_imports(self) -> tuple: """Return undetected browser imports""" from patchright.async_api import Page, Error from patchright.async_api import TimeoutError as PlaywrightTimeoutError - return Page, Error, PlaywrightTimeoutError \ No newline at end of file + return Page, Error, PlaywrightTimeoutError