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
feat(cli): add get command for info retrieval (title, html, text, value, attributes, bbox)
ShawnPana9abfc327cc0a
diff --git a/browser_use/browser/events.py b/browser_use/browser/events.pyindex 0d0f6f446..b3cd3b909 100644--- a/browser_use/browser/events.py+++ b/browser_use/browser/events.py@@ -560,6 +560,57 @@ class ElementRightClickEvent(ElementSelectedEvent[None]):event_timeout: float | None = Field(default_factory=lambda: _get_timeout('TIMEOUT_ElementRightClickEvent', 15.0))+# ============================================================================+# Information Retrieval Events+# ============================================================================+++class GetPageTitleEvent(BaseEvent[str]):+ """Get the page title."""++ event_timeout: float | None = Field(default_factory=lambda: _get_timeout('TIMEOUT_GetPageTitleEvent', 15.0))+++class GetPageHtmlEvent(BaseEvent[str]):+ """Get page HTML, optionally scoped to a selector."""++ selector: str | None = None # None = full document++ event_timeout: float | None = Field(default_factory=lambda: _get_timeout('TIMEOUT_GetPageHtmlEvent', 15.0))+++class GetElementTextEvent(ElementSelectedEvent[str]):+ """Get text content of an element."""++ node: 'EnhancedDOMTreeNode'++ event_timeout: float | None = Field(default_factory=lambda: _get_timeout('TIMEOUT_GetElementTextEvent', 15.0))+++class GetElementValueEvent(ElementSelectedEvent[str]):+ """Get value of an input/textarea element. Returns empty string if not found."""++ node: 'EnhancedDOMTreeNode'++ event_timeout: float | None = Field(default_factory=lambda: _get_timeout('TIMEOUT_GetElementValueEvent', 15.0))+++class GetElementAttributesEvent(ElementSelectedEvent[dict[str, str]]):+ """Get all attributes of an element."""++ node: 'EnhancedDOMTreeNode'++ event_timeout: float | None = Field(default_factory=lambda: _get_timeout('TIMEOUT_GetElementAttributesEvent', 15.0))+++class GetElementBoundingBoxEvent(ElementSelectedEvent[dict]):+ """Get bounding box of an element (x, y, width, height). Returns empty dict if not found."""++ node: 'EnhancedDOMTreeNode'++ event_timeout: float | None = Field(default_factory=lambda: _get_timeout('TIMEOUT_GetElementBoundingBoxEvent', 15.0))++# ============================================================================# Storage State Events# ============================================================================diff --git a/browser_use/browser/watchdogs/interactions_watchdog.py b/browser_use/browser/watchdogs/interactions_watchdog.pyindex 397f7fa96..6de887b27 100644--- a/browser_use/browser/watchdogs/interactions_watchdog.py+++ b/browser_use/browser/watchdogs/interactions_watchdog.py@@ -1,12 +1,19 @@-"""Interactions watchdog for hover, double-click, right-click."""+"""Interactions watchdog for hover, double-click, right-click, and info retrieval."""import asyncio+import jsonfrom typing import Any, ClassVarfrom bubus import BaseEventfrom browser_use.browser.events import (ElementDblClickEvent,+ GetElementAttributesEvent,+ GetElementBoundingBoxEvent,+ GetElementTextEvent,+ GetElementValueEvent,+ GetPageHtmlEvent,+ GetPageTitleEvent,HoverElementEvent,ElementRightClickEvent,)@@ -17,15 +24,25 @@ from browser_use.dom.service import EnhancedDOMTreeNodeHoverElementEvent.model_rebuild()ElementDblClickEvent.model_rebuild()ElementRightClickEvent.model_rebuild()+GetElementTextEvent.model_rebuild()+GetElementValueEvent.model_rebuild()+GetElementAttributesEvent.model_rebuild()+GetElementBoundingBoxEvent.model_rebuild()class InteractionsWatchdog(BaseWatchdog):- """Handles hover, double-click, and right-click interactions."""+ """Handles hover, double-click, right-click, and element info retrieval."""LISTENS_TO: ClassVar[list[type[BaseEvent]]] = [HoverElementEvent,ElementDblClickEvent,ElementRightClickEvent,+ GetPageTitleEvent,+ GetPageHtmlEvent,+ GetElementTextEvent,+ GetElementValueEvent,+ GetElementAttributesEvent,+ GetElementBoundingBoxEvent,]EMITS: ClassVar[list[type[BaseEvent]]] = []@@ -174,3 +191,121 @@ class InteractionsWatchdog(BaseWatchdog):self.logger.debug(f'[InteractionsWatchdog] Right-clicked at ({center_x}, {center_y})')except Exception as e:self.logger.error(f'[InteractionsWatchdog] Failed to right-click: {e}')++ async def _execute_js(self, js: str) -> Any:+ """Execute JavaScript in the browser context."""+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=None)+ if not cdp_session:+ raise RuntimeError('No active browser session')++ result = await cdp_session.cdp_client.send.Runtime.evaluate(+ params={'expression': js, 'returnByValue': True},+ session_id=cdp_session.session_id,+ )+ return result.get('result', {}).get('value')++ async def on_GetPageTitleEvent(self, event: GetPageTitleEvent) -> str:+ """Get the page title."""+ try:+ title = await self._execute_js('document.title')+ return title or ''+ except Exception as e:+ self.logger.error(f'[InteractionsWatchdog] Failed to get title: {e}')+ return ''++ async def on_GetPageHtmlEvent(self, event: GetPageHtmlEvent) -> str:+ """Get page HTML, optionally scoped to a selector."""+ try:+ if event.selector:+ js = f'''+ (function() {{+ const el = document.querySelector({json.dumps(event.selector)});+ return el ? el.outerHTML : null;+ }})()+ '''+ else:+ js = 'document.documentElement.outerHTML'++ html = await self._execute_js(js)+ return html or ''+ except Exception as e:+ self.logger.error(f'[InteractionsWatchdog] Failed to get HTML: {e}')+ return ''++ async def on_GetElementTextEvent(self, event: GetElementTextEvent) -> str:+ """Get text content of an element."""+ try:+ # Use the node's text from our model+ text = event.node.get_all_children_text(max_depth=10) if event.node else ''+ return text+ except Exception as e:+ self.logger.error(f'[InteractionsWatchdog] Failed to get element text: {e}')+ return ''++ async def on_GetElementValueEvent(self, event: GetElementValueEvent) -> str:+ """Get value of an input/textarea element."""+ try:+ cdp_session = await self.browser_session.cdp_client_for_node(event.node)+ backend_node_id = event.node.backend_node_id++ # Use callFunctionOn to get the value+ result = await cdp_session.cdp_client.send.DOM.resolveNode(+ params={'backendNodeId': backend_node_id},+ session_id=cdp_session.session_id,+ )+ object_id = result.get('object', {}).get('objectId')++ if object_id:+ value_result = await cdp_session.cdp_client.send.Runtime.callFunctionOn(+ params={+ 'objectId': object_id,+ 'functionDeclaration': 'function() { return this.value; }',+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )+ value = value_result.get('result', {}).get('value')+ return value or ''+ else:+ return ''+ except Exception as e:+ self.logger.error(f'[InteractionsWatchdog] Failed to get element value: {e}')+ return ''++ async def on_GetElementAttributesEvent(self, event: GetElementAttributesEvent) -> dict[str, Any]:+ """Get all attributes of an element."""+ try:+ # Use the attributes from the node model+ attrs = event.node.attributes or {}+ return dict(attrs)+ except Exception as e:+ self.logger.error(f'[InteractionsWatchdog] Failed to get element attributes: {e}')+ return {}++ async def on_GetElementBoundingBoxEvent(self, event: GetElementBoundingBoxEvent) -> dict[str, float]:+ """Get bounding box of an element."""+ try:+ cdp_session = await self.browser_session.cdp_client_for_node(event.node)+ backend_node_id = event.node.backend_node_id++ # Get box model+ result = await cdp_session.cdp_client.send.DOM.getBoxModel(+ params={'backendNodeId': backend_node_id},+ session_id=cdp_session.session_id,+ )++ model = result.get('model', {})+ content = model.get('content', [])++ if len(content) >= 8:+ # content is [x1, y1, x2, y2, x3, y3, x4, y4] - corners of the quad+ x = min(content[0], content[2], content[4], content[6])+ y = min(content[1], content[3], content[5], content[7])+ width = max(content[0], content[2], content[4], content[6]) - x+ height = max(content[1], content[3], content[5], content[7]) - y+ return {'x': x, 'y': y, 'width': width, 'height': height}+ else:+ return {}+ except Exception as e:+ self.logger.error(f'[InteractionsWatchdog] Failed to get element bbox: {e}')+ return {}diff --git a/browser_use/skill_cli/commands/browser.py b/browser_use/skill_cli/commands/browser.pyindex 3deae28cc..7486b8be6 100644--- a/browser_use/skill_cli/commands/browser.py+++ b/browser_use/skill_cli/commands/browser.py@@ -29,6 +29,7 @@ COMMANDS = {'hover','dblclick','rightclick',+ 'get',}@@ -293,4 +294,74 @@ async def handle(action: str, session: SessionInfo, params: dict[str, Any]) -> Areturn {'error': 'Invalid wait command. Use: selector, text'}+ elif action == 'get':+ get_command = params.get('get_command')++ if get_command == 'title':+ from browser_use.browser.events import GetPageTitleEvent++ event = GetPageTitleEvent()+ await bs.event_bus.dispatch(event)+ title = await event.event_result()+ return {'title': title}++ elif get_command == 'html':+ from browser_use.browser.events import GetPageHtmlEvent++ selector = params.get('selector')+ event = GetPageHtmlEvent(selector=selector)+ await bs.event_bus.dispatch(event)+ html = await event.event_result()+ return {'html': html}++ elif get_command == 'text':+ from browser_use.browser.events import GetElementTextEvent++ index = params['index']+ node = await bs.get_element_by_index(index)+ if node is None:+ return {'error': f'Element index {index} not found - page may have changed'}+ event = GetElementTextEvent(node=node)+ await bs.event_bus.dispatch(event)+ text = await event.event_result()+ return {'index': index, 'text': text}++ elif get_command == 'value':+ from browser_use.browser.events import GetElementValueEvent++ index = params['index']+ node = await bs.get_element_by_index(index)+ if node is None:+ return {'error': f'Element index {index} not found - page may have changed'}+ event = GetElementValueEvent(node=node)+ await bs.event_bus.dispatch(event)+ value = await event.event_result()+ return {'index': index, 'value': value}++ elif get_command == 'attributes':+ from browser_use.browser.events import GetElementAttributesEvent++ index = params['index']+ node = await bs.get_element_by_index(index)+ if node is None:+ return {'error': f'Element index {index} not found - page may have changed'}+ event = GetElementAttributesEvent(node=node)+ await bs.event_bus.dispatch(event)+ attrs = await event.event_result()+ return {'index': index, 'attributes': attrs}++ elif get_command == 'bbox':+ from browser_use.browser.events import GetElementBoundingBoxEvent++ index = params['index']+ node = await bs.get_element_by_index(index)+ if node is None:+ return {'error': f'Element index {index} not found - page may have changed'}+ event = GetElementBoundingBoxEvent(node=node)+ await bs.event_bus.dispatch(event)+ bbox = await event.event_result()+ return {'index': index, 'bbox': bbox}++ return {'error': 'Invalid get command. Use: title, html, text, value, attributes, bbox'}+raise ValueError(f'Unknown browser action: {action}')diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.pyindex 8a7befec7..7f71955b6 100755--- a/browser_use/skill_cli/main.py+++ b/browser_use/skill_cli/main.py@@ -320,6 +320,36 @@ Examples:p.add_argument('text', help='Text to wait for')p.add_argument('--timeout', type=int, default=30000, help='Timeout in ms')+ # -------------------------------------------------------------------------+ # Get Commands (info retrieval)+ # -------------------------------------------------------------------------++ get_p = subparsers.add_parser('get', help='Get information')+ get_sub = get_p.add_subparsers(dest='get_command')++ # get title+ get_sub.add_parser('title', help='Get page title')++ # get html [--selector SELECTOR]+ p = get_sub.add_parser('html', help='Get page HTML')+ p.add_argument('--selector', help='CSS selector to scope HTML')++ # get text <index>+ p = get_sub.add_parser('text', help='Get element text')+ p.add_argument('index', type=int, help='Element index')++ # get value <index>+ p = get_sub.add_parser('value', help='Get input element value')+ p.add_argument('index', type=int, help='Element index')++ # get attributes <index>+ p = get_sub.add_parser('attributes', help='Get element attributes')+ p.add_argument('index', type=int, help='Element index')++ # get bbox <index>+ p = get_sub.add_parser('bbox', help='Get element bounding box')+ p.add_argument('index', type=int, help='Element index')+# -------------------------------------------------------------------------# Python Execution# -------------------------------------------------------------------------diff --git a/skills/browser-use/SKILL.md b/skills/browser-use/SKILL.mdindex d5180e7e2..d8c4b859b 100644--- a/skills/browser-use/SKILL.md+++ b/skills/browser-use/SKILL.md@@ -120,6 +120,17 @@ browser-use dblclick <index> # Double-click elementbrowser-use rightclick <index> # Right-click element (context menu)```+### Information Retrieval+```bash+browser-use get title # Get page title+browser-use get html # Get full page HTML+browser-use get html --selector "h1" # Get HTML of specific element+browser-use get text <index> # Get text content of element+browser-use get value <index> # Get value of input/textarea+browser-use get attributes <index> # Get all attributes of element+browser-use get bbox <index> # Get bounding box (x, y, width, height)+```+### Python Execution (Persistent Session)```bashbrowser-use python "x = 42" # Set variable