Skip to content

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

add timeouts around occasionally slow getLayoutMetrics and always focus before getting layout

Nick Sweetingc5d33b4537bd
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.py
index b12e20e3b..f787d2eaa 100644
--- a/browser_use/browser/default_action_watchdog.py
+++ b/browser_use/browser/default_action_watchdog.py
@@ -230,7 +230,9 @@ class DefaultActionWatchdog(BaseWatchdog):
element_type = element_node.attributes.get('type', '').lower() if element_node.attributes else ''
if tag_name == 'select':
- self.logger.warning(f'Cannot click on <select> elements. Use get_dropdown_options(index={element_node.element_index}) action instead.')
+ self.logger.warning(
+ f'Cannot click on <select> elements. Use get_dropdown_options(index={element_node.element_index}) action instead.'
+ )
raise Exception(
f'<llm_error_msg>Cannot click on <select> elements. Use get_dropdown_options(index={element_node.element_index}) action instead.</llm_error_msg>'
)
@@ -241,7 +243,7 @@ class DefaultActionWatchdog(BaseWatchdog):
)
# Get CDP client
- cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=element_node.target_id, focus=False)
+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=element_node.target_id, focus=True)
# Get the correct session ID for the element's frame
session_id = cdp_session.session_id
diff --git a/browser_use/browser/dom_watchdog.py b/browser_use/browser/dom_watchdog.py
index 31e535c4d..330043c44 100644
--- a/browser_use/browser/dom_watchdog.py
+++ b/browser_use/browser/dom_watchdog.py
@@ -488,10 +488,14 @@ class DOMWatchdog(BaseWatchdog):
if not self.browser_session.agent_focus:
raise RuntimeError('No active CDP session - browser may not be connected yet')
- cdp_session = self.browser_session.agent_focus
+ cdp_session = await self.browser_session.get_or_create_cdp_session(
+ target_id=self.browser_session.agent_focus.target_id, focus=True
+ )
# Get layout metrics which includes all the information we need
- metrics = await cdp_session.cdp_client.send.Page.getLayoutMetrics(session_id=cdp_session.session_id)
+ metrics = await asyncio.wait_for(
+ cdp_session.cdp_client.send.Page.getLayoutMetrics(session_id=cdp_session.session_id), timeout=10.0
+ )
# Extract different viewport types
layout_viewport = metrics.get('layoutViewport', {})
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 24a686cc5..3e1e22e85 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -141,8 +141,8 @@ class CDPSession(BaseModel):
# disable breakpoints on the page so it doesnt pause on crashes / debugger statements
try:
await self.cdp_client.send.Debugger.setSkipAllPauses(params={'skip': True}, session_id=self.session_id)
- if 'Debugger' not in domains:
- await self.cdp_client.send.Debugger.disable()
+ # if 'Debugger' not in domains:
+ # await self.cdp_client.send.Debugger.disable()
# await cdp_session.cdp_client.send.EventBreakpoints.disable(session_id=cdp_session.session_id)
except Exception as e:
# self.logger.warning(f'Failed to disable page JS breakpoints: {e}')
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index a899d713a..bae957f9c 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -124,7 +124,7 @@ class DomService:
async def _get_viewport_ratio(self, target_id: str) -> float:
"""Get viewport dimensions, device pixel ratio, and scroll position using CDP."""
- cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=target_id, focus=False)
+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=target_id, focus=True)
try:
# Get the layout metrics which includes the visual viewport