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
Element text after click
Magnus Müller1947eab03ef7
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.pyindex 9f84d07ee..f77bfa726 100644--- a/browser_use/tools/service.py+++ b/browser_use/tools/service.py@@ -33,6 +33,7 @@ from browser_use.llm.base import BaseChatModelfrom browser_use.llm.messages import SystemMessage, UserMessagefrom browser_use.observability import observe_debugfrom browser_use.tools.registry.service import Registry+from browser_use.tools.utils import get_click_descriptionfrom browser_use.tools.views import (ClickElementAction,CloseTabAction,@@ -241,7 +242,7 @@ class Tools(Generic[Context]):# Dispatch click event with nodetry:assert params.index != 0, (- 'Cannot click on element with index 0. If there are no interactive elements use scroll(), wait(), refresh(), etc. to troubleshoot'+ 'Cannot click on element with index 0. If there are no interactive elements use wait(), refresh(), etc. to troubleshoot')# Look up the node from the selector map@@ -251,6 +252,9 @@ class Tools(Generic[Context]):logger.warning(f'⚠️ {msg}')return ActionResult(extracted_content=msg)+ # Get description of clicked element+ element_desc = get_click_description(node)+# Highlight the element being clicked (truly non-blocking)asyncio.create_task(browser_session.highlight_interaction_element(node))@@ -258,10 +262,10 @@ class Tools(Generic[Context]):await event# Wait for handler to complete and get any exception or metadataclick_metadata = await event.event_result(raise_if_any=True, raise_if_none=False)- memory = 'Clicked element'- msg = f'🖱️ {memory}'- logger.info(msg)+ # Build memory with element info+ memory = f'Clicked {element_desc}'+ logger.info(f'🖱️ {memory}')# Include click coordinates in metadata if availablereturn ActionResult(@@ -570,7 +574,7 @@ class Tools(Generic[Context]):# This action is temporarily disabled as it needs refactoring to use events@self.registry.action(- """LLM extracts structured data from page markdown. Use when: on right page, know what to extract, haven't called before on same page+query. Can't get interactive elements. Set extract_links=True for URLs. Use start_from_char if truncated. If fails, use find_text/scroll instead.""",+ """LLM extracts structured data from page markdown. Use when: on right page, know what to extract, haven't called before on same page+query. Can't get interactive elements. Set extract_links=True for URLs. Use start_from_char if truncated. If fails, use find_text instead.""",)async def extract(query: str,diff --git a/browser_use/tools/utils.py b/browser_use/tools/utils.pynew file mode 100644index 000000000..ad839bf29--- /dev/null+++ b/browser_use/tools/utils.py@@ -0,0 +1,82 @@+"""Utility functions for browser tools."""++from browser_use.dom.service import EnhancedDOMTreeNode+++def get_click_description(node: EnhancedDOMTreeNode) -> str:+ """Get a brief description of the clicked element for memory."""+ parts = []++ # Tag name+ parts.append(node.tag_name)++ # Add type for inputs+ if node.tag_name == 'input' and node.attributes.get('type'):+ input_type = node.attributes['type']+ parts.append(f'type={input_type}')++ # For checkboxes, include checked state+ if input_type == 'checkbox':+ is_checked = node.attributes.get('checked', 'false').lower() in ['true', 'checked', '']+ # Also check AX node+ if node.ax_node and node.ax_node.properties:+ for prop in node.ax_node.properties:+ if prop.name == 'checked':+ is_checked = prop.value is True or prop.value == 'true'+ break+ state = 'checked' if is_checked else 'unchecked'+ parts.append(f'checkbox-state={state}')++ # Add role if present+ if node.attributes.get('role'):+ role = node.attributes['role']+ parts.append(f'role={role}')++ # For role=checkbox, include state+ if role == 'checkbox':+ aria_checked = node.attributes.get('aria-checked', 'false').lower()+ is_checked = aria_checked in ['true', 'checked']+ if node.ax_node and node.ax_node.properties:+ for prop in node.ax_node.properties:+ if prop.name == 'checked':+ is_checked = prop.value is True or prop.value == 'true'+ break+ state = 'checked' if is_checked else 'unchecked'+ parts.append(f'checkbox-state={state}')++ # For labels/spans/divs, check if related to a hidden checkbox+ if node.tag_name in ['label', 'span', 'div'] and 'type=' not in ' '.join(parts):+ # Check children for hidden checkbox+ for child in node.children:+ if child.tag_name == 'input' and child.attributes.get('type') == 'checkbox':+ # Check if hidden+ is_hidden = False+ if child.snapshot_node and child.snapshot_node.computed_styles:+ opacity = child.snapshot_node.computed_styles.get('opacity', '1')+ if opacity == '0' or opacity == '0.0':+ is_hidden = True++ if is_hidden or not child.is_visible:+ # Get checkbox state+ is_checked = child.attributes.get('checked', 'false').lower() in ['true', 'checked', '']+ if child.ax_node and child.ax_node.properties:+ for prop in child.ax_node.properties:+ if prop.name == 'checked':+ is_checked = prop.value is True or prop.value == 'true'+ break+ state = 'checked' if is_checked else 'unchecked'+ parts.append(f'checkbox-state={state}')+ break++ # Add short text content if available+ text = node.get_all_children_text().strip()+ if text:+ short_text = text[:30] + ('...' if len(text) > 30 else '')+ parts.append(f'"{short_text}"')++ # Add key attributes like id, name, aria-label+ for attr in ['id', 'name', 'aria-label']:+ if node.attributes.get(attr):+ parts.append(f'{attr}={node.attributes[attr][:20]}')++ return ' '.join(parts)