Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Re-using context when cdp is used

erikmuttersbach24c9a7c9f9d0
diff --git a/browser_use/browser/context.py b/browser_use/browser/context.py
index 2d2c3c827..5e8fac90b 100644
--- a/browser_use/browser/context.py
+++ b/browser_use/browser/context.py
@@ -221,7 +221,9 @@ class BrowserContext:
async def _create_context(self, browser: PlaywrightBrowser):
"""Creates a new browser context with anti-detection measures and loads cookies if available."""
- if self.browser.config.chrome_instance_path and len(browser.contexts) > 0:
+ if self.browser.config.cdp_url:
+ context = browser.contexts[0]
+ elif self.browser.config.chrome_instance_path and len(browser.contexts) > 0:
# Connect to existing Chrome instance instead of creating new one
context = browser.contexts[0]
else: