gregpr07 / browser-use

Make websites accessible for AI agents
https://link.browser-use.com/discord
MIT License
2.43k stars 174 forks source link

[REQUEST] Use persistent browser context as option #51

Open skye0402 opened 1 day ago

skye0402 commented 1 day ago

Currently Playwright is opening a new browser context every time. This can be useful but all cookies, authentication and caching will be done from scratch.

I propose an option to start it with context. e.g. with parameters

use_context: bool # Default can be False
user_data_dir: str # The path to the folder that holds the browser data (Playwright creates it at first use)

I did some quick fix and it works well in browser/service.py

async def _setup_browser(self, playwright: Playwright) -> PlaywrightBrowser:
        """Sets up and returns a Playwright Browser instance with anti-detection measures."""
        try:
            user_data_dir = os.path.join(os.getcwd(), 'playwright_user_data')
            browser = await playwright.chromium.launch_persistent_context(
                user_data_dir=user_data_dir,
                headless=self.headless,
                viewport={'width': 1280, 'height': 1024},
                ignore_default_args=['--enable-automation'],  # Helps with anti-detection
                args=[
                    '--no-sandbox',
                    '--disable-blink-features=AutomationControlled',
                    '--disable-extensions',
                    '--disable-infobars',
                    '--disable-background-timer-throttling',
                    '--disable-popup-blocking',
                    '--disable-backgrounding-occluded-windows',
                    '--disable-renderer-backgrounding',
                    '--disable-window-activation',
                    '--disable-focus-on-load',  # Prevents focus on navigation
                    '--no-first-run',
                    '--no-default-browser-check',
                    # '--no-startup-window',  # Prevents initial focus
                    '--window-position=0,0',
                ],
            )

            return browser
        except Exception as e:
            logger.error(f'Failed to initialize Playwright browser: {str(e)}')
            raise

    async def _create_context(self, browser: Union[PlaywrightBrowser, BrowserContext])::
        """Creates a new browser context with anti-detection measures."""
        if isinstance(browser, BrowserContext): # to check if it's a Browser or BrowserContext object
            context = browser
        else:
            context = await browser.new_context(
                viewport={'width': 1280, 'height': 1024},
            user_agent=(
                'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                '(KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
            ),
            java_script_enabled=True,
        )

Of course it needs the proper switching in _setup_browser - as you can see the --no-startup-window is commented out, it would lead to an endless loop. However, it works well without it.

What do you think? Loading is much, much faster now.

MagMueller commented 1 day ago

Thank you very much. This stores all cookies automatically?

and you mean when use_context is false it does not use launch_persistent_context

skye0402 commented 1 day ago

@MagMueller yes, 100%. Default should be as it's now. For users that want persistence between the sessions they need to specify a folder and state True. It behaves then like our usual browser with cache and cookies. However, certificate prompts still pop up. Need to look into this later (different issue).

MagMueller commented 18 hours ago

Great - can you do a PR with your version?

Do you know good ways do directly integrate addblockers ?