Skip to content

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

More time calculations

magmueller52cf8402b55c
diff --git a/src/agent/prompts.py b/src/agent/prompts.py
index a6a7bc139..cefb2b110 100644
--- a/src/agent/prompts.py
+++ b/src/agent/prompts.py
@@ -1,6 +1,7 @@
from langchain_core.messages import HumanMessage, SystemMessage
from src.controller.views import ControllerPageState
+from src.utils import time_execution_sync
class AgentSystemPrompt:
@@ -62,6 +63,7 @@ class AgentMessagePrompt:
def __init__(self, state: ControllerPageState):
self.state = state
+ @time_execution_sync('get_user_message')
def get_user_message(self) -> HumanMessage:
state_description = f"""
Current url: {self.state.url}
diff --git a/src/browser/service.py b/src/browser/service.py
index 7d25642a4..b9a529fc6 100644
--- a/src/browser/service.py
+++ b/src/browser/service.py
@@ -320,6 +320,7 @@ class BrowserService:
except Exception as e:
raise Exception(f'Failed to click element with xpath: {xpath}. Error: {str(e)}')
+ @time_execution_sync('click')
def click_element_by_index(self, index: int, state: BrowserState):
"""
Clicks an element using its index from the selector map.
diff --git a/src/controller/service.py b/src/controller/service.py
index 4f6caac14..63fa267bc 100644
--- a/src/controller/service.py
+++ b/src/controller/service.py
@@ -1,6 +1,7 @@
from src.browser.service import BrowserService
from src.browser.views import BrowserState
from src.controller.views import ControllerActionResult, ControllerActions, ControllerPageState
+from src.utils import time_execution_sync
class ControllerService:
@@ -41,6 +42,7 @@ class ControllerService:
screenshot=screenshot_b64,
)
+ @time_execution_sync('act')
def act(self, action: ControllerActions) -> ControllerActionResult:
try:
current_state = self.get_cached_browser_state(force_update=True)
diff --git a/src/dom/service.py b/src/dom/service.py
index 81b6a7bf5..07a129ea4 100644
--- a/src/dom/service.py
+++ b/src/dom/service.py
@@ -9,7 +9,8 @@ class DomService:
self.driver = driver
def get_clickable_elements(self) -> ProcessedDomContent:
- # Get current state of DOM by executing JavaScript
+ # Clear xpath cache on each new DOM processing
+ self.xpath_cache = {}
html_content = self.driver.page_source
return self._process_content(html_content)
@@ -253,6 +254,9 @@ class DomService:
if len(same_type_siblings) > 1:
try:
index = same_type_siblings.index(current) + 1
+ # print(
+ # f'Debug XPath: Found {len(same_type_siblings)} siblings of type {current.name}, current index: {index}'
+ # )
parts.insert(0, f'{current.name}[{index}]')
except ValueError:
parts.insert(0, current.name)
diff --git a/src/dom/tests/extraction_test.py b/src/dom/tests/extraction_test.py
index dab2ebac5..68d47471d 100644
--- a/src/dom/tests/extraction_test.py
+++ b/src/dom/tests/extraction_test.py
@@ -5,8 +5,8 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from tokencost import count_string_tokens
-from src.browser.service import BrowserService
+from src.browser.service import BrowserService
from src.dom.service import DomService
from src.utils import time_execution_sync