Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix clicking and typing in iframes
Nick Sweeting140346b99e53
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.pyindex 62edcbdf0..5b5889867 100644--- a/browser_use/browser/default_action_watchdog.py+++ b/browser_use/browser/default_action_watchdog.py@@ -136,6 +136,10 @@ class DefaultActionWatchdog(BaseWatchdog):except Exception as e:# Element not found or error - fall back to typing to the pageself.logger.warning(f'Failed to type to element {index_for_logging}: {e}. Falling back to page typing.')+ try:+ await asyncio.wait_for(self._click_element_node_impl(element_node, while_holding_ctrl=False), timeout=3.0)+ except Exception as e:+ passawait self._type_to_page(event.text)self.logger.info(f'⌨️ Typed "{event.text}" to the page as fallback')@@ -248,8 +252,7 @@ class DefaultActionWatchdog(BaseWatchdog):)# Get CDP client- await self.browser_session.get_or_create_cdp_session(focus=True)- 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.cdp_client_for_node(element_node)# Get the correct session ID for the element's framesession_id = cdp_session.session_id@@ -675,14 +678,14 @@ class DefaultActionWatchdog(BaseWatchdog):# Get the correct session ID for the element's iframe# session_id = await self._get_session_id_for_element(element_node)- cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=element_node.target_id, focus=True)+ # cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=element_node.target_id, focus=True)+ cdp_session = await self.browser_session.cdp_client_for_node(element_node)# Get element infobackend_node_id = element_node.backend_node_id# Scroll element into viewtry:- await cdp_session.cdp_client.send.Target.activateTarget(params={'targetId': element_node.target_id})await cdp_session.cdp_client.send.DOM.scrollIntoViewIfNeeded(params={'backendNodeId': backend_node_id}, session_id=cdp_session.session_id)diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 574df8988..3b1b1f8e9 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -1744,20 +1744,43 @@ class BrowserSession(BaseModel):async def cdp_client_for_node(self, node: EnhancedDOMTreeNode) -> CDPSession:"""Get CDP client for a specific DOM node based on its frame."""if node.frame_id:- # If cross-origin iframes are disabled, always use the main session- if not self.browser_profile.cross_origin_iframes:- assert self.agent_focus is not None, 'No active CDP session'- return self.agent_focus+ # # If cross-origin iframes are disabled, always use the main session+ # if not self.browser_profile.cross_origin_iframes:+ # assert self.agent_focus is not None, 'No active CDP session'+ # return self.agent_focus# Otherwise, try to get the frame-specific sessiontry:- return await self.cdp_client_for_frame(node.frame_id)+ cdp_session = await self.cdp_client_for_frame(node.frame_id)+ result = await cdp_session.cdp_client.send.DOM.resolveNode(+ params={'backendNodeId': node.backend_node_id},+ session_id=cdp_session.session_id,+ )+ object_id = result.get('object', {}).get('objectId')+ if not object_id:+ raise ValueError(+ f'Could not find #{node.element_index} backendNodeId={node.backend_node_id} in target_id={cdp_session.target_id}'+ )+ return cdp_sessionexcept (ValueError, Exception) as e:# Fall back to main session if frame not foundself.logger.debug(f'Failed to get CDP client for frame {node.frame_id}: {e}, using main session')- assert self.agent_focus is not None, 'No active CDP session'- return self.agent_focus- assert self.agent_focus is not None, 'No active CDP session'- return self.agent_focus++ if node.target_id:+ try:+ cdp_session = await self.get_or_create_cdp_session(target_id=node.target_id, focus=False)+ result = await cdp_session.cdp_client.send.DOM.resolveNode(+ params={'backendNodeId': node.backend_node_id},+ session_id=cdp_session.session_id,+ )+ object_id = result.get('object', {}).get('objectId')+ if not object_id:+ raise ValueError(+ f'Could not find #{node.element_index} backendNodeId={node.backend_node_id} in target_id={cdp_session.target_id}'+ )+ except Exception as e:+ self.logger.debug(f'Failed to get CDP client for target {node.target_id}: {e}, using main session')++ return await self.get_or_create_cdp_session()# # Fix Pydantic circular dependency for all watchdogsdiff --git a/browser_use/browser/views.py b/browser_use/browser/views.pyindex 3469b3343..355c542ff 100644--- a/browser_use/browser/views.py+++ b/browser_use/browser/views.py@@ -141,8 +141,10 @@ class BrowserError(Exception):def __str__(self) -> str:if self.details:return f'{self.message} ({self.details}) during: {self.while_handling_event}'+ elif self.while_handling_event:+ return f'{self.message} (while handling: {self.while_handling_event})'else:- return f'{self.message} (while handling event: {self.while_handling_event})'+ return self.messageclass URLNotAllowedError(BrowserError):