From 30bff1407de44bd12a04a5d4d30da27cd7de808c Mon Sep 17 00:00:00 2001 From: kranthi419 Date: Wed, 29 Jan 2025 03:37:32 +0530 Subject: [PATCH] Added support for local browser use --- backend/app/webrover.py | 56 +++++++++++++++++++++++++++++------------ readme.md | 19 ++++++++++++-- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/backend/app/webrover.py b/backend/app/webrover.py index 43f0ab4..8f37bff 100644 --- a/backend/app/webrover.py +++ b/backend/app/webrover.py @@ -48,6 +48,11 @@ def set_env_vars(var): mark_page_path = os.path.join(current_dir, "static", "mark_page.js") +# Path to the local Chrome executable +chrome_path = os.getenv("CHROME_PATH") + +# Path to the existing Chrome user data directory +user_data_dir = os.getenv("CHROME_USER_DATA") class Bbox(TypedDict): @@ -203,23 +208,42 @@ async def setup_browser_2(go_to_page: str): "locale": 'en-US', "timezone_id": 'America/Los_Angeles', } + + if chrome_path and user_data_dir: + browser = await playwright.chromium.launch_persistent_context( + user_data_dir=user_data_dir, + executable_path=chrome_path, + headless=False, + args=browser_args, + **context_options + ) + + page = await browser.new_page() + + else: + if chrome_path: + browser = await playwright.chromium.launch( + headless=False, + executable_path=chrome_path, + args=browser_args + ) + else: + browser = await playwright.chromium.launch( + headless=False, + args=browser_args + ) - browser = await playwright.chromium.launch( - headless=False, - args=browser_args - ) - - # Create context with the specified options - context = await browser.new_context(**context_options) - - # Enable JavaScript and cookies - await context.add_init_script(""" - Object.defineProperty(navigator, 'webdriver', { - get: () => undefined - }); - """) - - page = await context.new_page() + # Create context with the specified options + context = await browser.new_context(**context_options) + + # Enable JavaScript and cookies + await context.add_init_script(""" + Object.defineProperty(navigator, 'webdriver', { + get: () => undefined + }); + """) + + page = await context.new_page() try: await page.goto(go_to_page, timeout=80000, wait_until="domcontentloaded") diff --git a/readme.md b/readme.md index 313f063..877fc5c 100644 --- a/readme.md +++ b/readme.md @@ -158,7 +158,22 @@ The agent comes equipped with several tools to interact with web pages: LANGCHAIN_PROJECT="your_project_name" ``` -5. Run the backend: +5. Using Your Own Browser(Optional): + - Set `CHROME_PATH` to the executable path of your browser and `CHROME_USER_DATA` to the user data directory of your browser. + - Windows + ```env + CHROME_PATH="C:\Program Files\Google\Chrome\Application\chrome.exe" + CHROME_USER_DATA="C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data" + ``` + > Note: Replace `YourUsername` with your actual Windows username for Windows systems. + - Mac + ```env + CHROME_PATH="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" + CHROME_USER_DATA="~/Library/Application Support/Google/Chrome/Profile 1" + ``` + - go to chrome://version/ in Chrome, find the path to your chrome executable and user data (Profile Path) directory. + +6. Run the backend: Make sure you are in the backend folder @@ -172,7 +187,7 @@ The agent comes equipped with several tools to interact with web pages: uvicorn app.main:app --port 8000 ``` -6. Access the API at `http://localhost:8000` +7. Access the API at `http://localhost:8000` ## Frontend Setup