CoolFace
Apppublic

aroniscunt/Browser_Web_UI_Automation

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
custom_context.py47 linesDownload Raw Back to browser
1import json
2import logging
3import os
4
5# Import browser_use components with comprehensive error handling
6try:
7    from browser_use.browser.browser import Browser, IN_DOCKER
8    from browser_use.browser.context import BrowserContext, BrowserContextConfig, BrowserContextState
9except ImportError as e:
10    # Fallback for different browser_use versions
11    logger = logging.getLogger(__name__)
12    logger.warning(f"Failed to import browser_use components: {e}")
13    
14    # Set default values
15    IN_DOCKER = False
16    
17    # Try to import basic components
18    try:
19        from browser_use.browser.browser import Browser
20        from browser_use.browser.context import BrowserContext, BrowserContextConfig
21        # Try to import BrowserContextState, but don't fail if it doesn't exist
22        try:
23            from browser_use.browser.context import BrowserContextState
24        except ImportError:
25            # Create a dummy BrowserContextState if it doesn't exist
26            class BrowserContextState:
27                pass
28    except ImportError:
29        logger.error("Could not import basic browser_use components")
30        raise
31
32from playwright.async_api import Browser as PlaywrightBrowser
33from playwright.async_api import BrowserContext as PlaywrightBrowserContext
34from typing import Optional
35
36logger = logging.getLogger(__name__)
37
38
39class CustomBrowserContext(BrowserContext):
40    def __init__(
41            self,
42            browser: 'Browser',
43            config: BrowserContextConfig | None = None,
44            state: Optional[BrowserContextState] = None,
45    ):
46        super(CustomBrowserContext, self).__init__(browser=browser, config=config, state=state)
47