Skip to content

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

feat: add Chrome profile CLI options for local browser profiles

TCaac14e17a832
diff --git a/browser_use/cli.py b/browser_use/cli.py
index 6403e7c6b..9697096e1 100644
--- a/browser_use/cli.py
+++ b/browser_use/cli.py
@@ -160,6 +160,12 @@ def update_config_with_click_args(config: dict[str, Any], ctx: click.Context) ->
config['browser']['window_width'] = ctx.params['window_width']
if ctx.params.get('window_height'):
config['browser']['window_height'] = ctx.params['window_height']
+ if ctx.params.get('user_data_dir'):
+ config['browser']['user_data_dir'] = ctx.params['user_data_dir']
+ if ctx.params.get('profile_directory'):
+ config['browser']['profile_directory'] = ctx.params['profile_directory']
+ if ctx.params.get('cdp_url'):
+ config['browser']['cdp_url'] = ctx.params['cdp_url']
return config
@@ -1170,13 +1176,9 @@ async def run_prompt_mode(prompt: str, ctx: click.Context, debug: bool = False):
# Get agent settings from config
agent_settings = AgentSettings.model_validate(config.get('agent', {}))
- # Create browser session with headless=True and no user_data_dir
- browser_session = BrowserSession(
- headless=False,
- # user_data_dir=None,
- # playwright=(await async_playwright().start()),
- # channel=BrowserChannel.CHROME,
- )
+ # Create browser session with config parameters
+ browser_config = config.get('browser', {})
+ browser_session = BrowserSession(**browser_config)
# Create and run agent
agent = Agent(
@@ -1185,7 +1187,6 @@ async def run_prompt_mode(prompt: str, ctx: click.Context, debug: bool = False):
browser_session=browser_session,
source='cli',
**agent_settings.model_dump(),
- # Run the agent
)
await agent.run()
@@ -1316,10 +1317,23 @@ async def textual_interface(config: dict[str, Any]):
@click.option('--headless', is_flag=True, help='Run browser in headless mode', default=None)
@click.option('--window-width', type=int, help='Browser window width')
@click.option('--window-height', type=int, help='Browser window height')
[email protected]('--user-data-dir', type=str, help='Path to Chrome user data directory (e.g., ~/Library/Application Support/Google/Chrome)')
[email protected]('--profile-directory', type=str, help='Chrome profile directory name (e.g., "Default", "Profile 1")')
[email protected]('--cdp-url', type=str, help='Connect to existing Chrome via CDP URL (e.g., http://localhost:9222)')
@click.option('-p', '--prompt', type=str, help='Run a single task without the TUI (headless mode)')
@click.pass_context
def main(ctx: click.Context, debug: bool = False, **kwargs):
- """Browser-Use Interactive TUI or Command Line Executor"""
+ """Browser-Use Interactive TUI or Command Line Executor
+
+ Use --user-data-dir to specify a local Chrome profile directory.
+ Common Chrome profile locations:
+ macOS: ~/Library/Application Support/Google/Chrome
+ Linux: ~/.config/google-chrome
+ Windows: %LOCALAPPDATA%\\Google\\Chrome\\User Data
+
+ Use --profile-directory to specify which profile within the user data directory.
+ Examples: "Default", "Profile 1", "Profile 2", etc.
+ """
if kwargs['version']:
from importlib.metadata import version
diff --git a/examples/features/chrome_profile_example.py b/examples/features/chrome_profile_example.py
new file mode 100644
index 000000000..f7aaca5cc
--- /dev/null
+++ b/examples/features/chrome_profile_example.py
@@ -0,0 +1,43 @@
+"""
+Example of using Chrome profiles with browser-use.
+
+This example demonstrates how to use existing Chrome profiles with saved logins and extensions.
+"""
+
+import asyncio
+from browser_use import Agent
+from browser_use.browser import BrowserSession, BrowserConfig
+
+
+async def main():
+ # Example 1: Use a specific Chrome profile with saved logins
+ browser_session = BrowserSession(
+ user_data_dir="~/Library/Application Support/Google/Chrome",
+ profile_directory="Profile 1" # Use "Default" for the default profile
+ )
+
+ agent = Agent(
+ task="Check my Gmail inbox",
+ browser_session=browser_session
+ )
+
+ await agent.run()
+
+ # Example 2: Connect to an existing Chrome instance via CDP
+ # First, start Chrome with:
+ # /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
+
+ browser_session_cdp = BrowserSession(
+ cdp_url="http://localhost:9222"
+ )
+
+ agent_cdp = Agent(
+ task="Navigate to GitHub and show my profile",
+ browser_session=browser_session_cdp
+ )
+
+ await agent_cdp.run()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
\ No newline at end of file