Skip to content

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

Take screen shot of window or entire page

magmuellere4b51212cb73
diff --git a/src/driver/service.py b/src/driver/service.py
index db63fbaeb..1b27bbba8 100644
--- a/src/driver/service.py
+++ b/src/driver/service.py
@@ -31,6 +31,8 @@ class DriverService:
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_experimental_option('useAutomationExtension', False)
+ chrome_options.add_argument('--start-maximized')
+ chrome_options.add_argument('--force-device-scale-factor=1')
# Initialize the Chrome driver
driver = webdriver.Chrome(
diff --git a/src/state_manager/state.py b/src/state_manager/state.py
index fba75c5f2..29b6c3aeb 100644
--- a/src/state_manager/state.py
+++ b/src/state_manager/state.py
@@ -2,6 +2,7 @@ from typing import Dict, List
import requests
from pydantic import BaseModel
+from Screenshot import Screenshot
from selenium import webdriver
from src.dom.service import DomService
@@ -17,14 +18,16 @@ class PageState(BaseModel):
page_title: str
interactable_elements: str
selector_map: Dict[int, str]
+ screenshot: str
class StateManager:
def __init__(self, driver: webdriver.Chrome):
self.driver = driver
self.dom_service = DomService(driver)
+ self.ob = Screenshot.Screenshot()
- def get_current_state(self) -> PageState:
+ def get_current_state(self, run_folder: str, full_page_screenshot: bool = False) -> PageState:
"""
Retrieves current URL and interactable elements from processed HTML.
@@ -33,11 +36,27 @@ class StateManager:
"""
processed_content = self.dom_service.get_current_state()
+ if full_page_screenshot:
+ screenshot = self.ob.full_screenshot(
+ self.driver,
+ save_path=run_folder,
+ image_name='selenium_full_screenshot.png',
+ is_load_at_runtime=True,
+ load_wait_time=0,
+ )
+ screenshot = run_folder + '/selenium_full_screenshot.png'
+ else:
+ file_name = run_folder + '/window_screenshot.png'
+ screenshot = self.driver.get_screenshot_as_file(file_name)
+ screenshot = file_name if screenshot else ''
+
+ print(screenshot)
return PageState(
current_url=self.driver.current_url,
page_title=self.driver.title,
interactable_elements=processed_content.output_string,
selector_map=processed_content.selector_map,
+ screenshot=screenshot,
)
def get_functions(self) -> List[Dict]:
diff --git a/src/tests/test_kayak_search.py b/src/tests/test_kayak_search.py
index 8c2d78d8f..44f24b4cd 100644
--- a/src/tests/test_kayak_search.py
+++ b/src/tests/test_kayak_search.py
@@ -6,7 +6,7 @@ import pytest
from src.actions.browser_actions import ActionResult, BrowserActions
from src.agent_interface.planing_agent import PlaningAgent
from src.driver.service import DriverService
-from src.state_manager.state import StateManager
+from src.state_manager.state import PageState, StateManager
from src.state_manager.utils import save_formatted_html
@@ -28,6 +28,7 @@ async def test_kayak_flight_search(setup):
# task = 'apply for a job at meta internship for ml research in sunny vale'
timestamp = 'flight_search_bali_to_kirgistan'
task = 'find a flight from Bali to Kirgistan on 2024-11-25 for 2 people one way.'
+ vision = True
run_folder = f'temp/{timestamp}'
if not os.path.exists(run_folder):
@@ -48,12 +49,25 @@ async def test_kayak_flight_search(setup):
max_steps = 50
for i in range(max_steps):
print(f'\n📍 Step {i+1}')
- current_state = state_manager.get_current_state()
+ current_state: PageState = state_manager.get_current_state(run_folder)
save_formatted_html(
current_state.interactable_elements, f'{run_folder}/current_state_{i}.html'
)
# save normal html
save_formatted_html(driver.page_source, f'{run_folder}/html_{i}.html')
+
+ # show image and pause
+ # if vision:
+ # import matplotlib.pyplot as plt
+
+ # # read path and show image
+ # # image = plt.imread(current_state.screenshot)
+ # # # no axis
+ # # plt.axis('off')
+
+ # # plt.imshow(image)
+ # plt.pause(5)
+
url_history.append(driver.current_url)
state_text = f'Current interactive elements: {current_state.interactable_elements}'
diff --git a/temp/run_bali_flight_search/conversation_9.txt b/temp/run_bali_flight_search/conversation_9.txt
index 76086efce..8481b5591 100644
--- a/temp/run_bali_flight_search/conversation_9.txt
+++ b/temp/run_bali_flight_search/conversation_9.txt
@@ -34,6 +34,7 @@ user: Your task is: find a flight from Zürich to (ask the user for the destina
user: bali
assistant: {"action":"ask_user","params":{"text":"Please provide the destination city for the flight you want to book from Zürich.","url":null,"id":null},"goal":"Get the user to specify the destination city for the flight.","valuation_previous_goal":"Unable to proceed without knowing the destination city."}
assistant: {"action":"search_google","params":{"text":"flight from Zürich to Bali on 2024-11-25 with return on 2024-12-09 for 2 people booking site.","url":null,"id":null},"goal":"Find a website to book a flight from Zürich to Bali on the specified dates.","valuation_previous_goal":"Success as the destination input was successfully obtained from the user."}
+user: ... state
assistant: {"action":"click","params":{"text":null,"url":null,"id":19},"goal":"Navigate to the ebookers.ch webpage to look for flight options from Zürich to Bali.","valuation_previous_goal":"Unsuccessful as I need to navigate to a specific website to find flights."}
assistant: {"action":"click","params":{"text":null,"url":null,"id":76},"goal":"Search for flight options from Zürich to Bali on the specified dates.","valuation_previous_goal":"Success as we have navigated to the ebookers.ch website, where flight options can be searched."}
assistant: {"action":"click","params":{"text":null,"url":null,"id":10},"goal":"Accept cookies to proceed smoothly with the flight search process.","valuation_previous_goal":"Necessary to accept or decline cookies to proceed with using the website effectively."}