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
make Click action more flexible to take over node and idx-based clicking
Nick Sweeting14c7d47fd908
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.pyindex 521ec30ea..8ab77d81a 100644--- a/browser_use/browser/default_action_watchdog.py+++ b/browser_use/browser/default_action_watchdog.py@@ -33,23 +33,29 @@ class DefaultActionWatchdog(BaseWatchdog):"""Handle click request with CDP."""page = await self.browser_session.get_current_page()try:- # Get the DOM element by index- element_node = await self.browser_session.get_dom_element_by_index(event.index)- if element_node is None:- raise Exception(f'Element index {event.index} does not exist - retry or use alternative actions')+ # Get the DOM element by index or use provided element_node+ if event.element_node is not None:+ element_node = event.element_node+ # For element_node clicks, we need to get its index for logging+ index_for_logging = getattr(element_node, 'highlight_index', 'N/A')+ else:+ element_node = await self.browser_session.get_dom_element_by_index(event.index)+ if element_node is None:+ raise Exception(f'Element index {event.index} does not exist - retry or use alternative actions')+ index_for_logging = event.index# Track initial number of tabs to detect new tab openinginitial_pages = len(self.browser_session.pages)# Check if element is a file input (should not be clicked)if self.browser_session.is_file_input(element_node):- msg = f'Index {event.index} - has an element which opens file upload dialog. To upload files please use a specific function to upload files'+ msg = f'Index {index_for_logging} - has an element which opens file upload dialog. To upload files please use a specific function to upload files'logger.info(msg)self.event_bus.dispatch(BrowserErrorEvent(error_type='FileInputElement',message=msg,- details={'index': event.index},+ details={'index': index_for_logging},))return@@ -64,7 +70,7 @@ class DefaultActionWatchdog(BaseWatchdog):msg = f'Downloaded file to {download_path}'logger.info(f'💾 {msg}')else:- msg = f'Clicked button with index {event.index}: {element_node.get_all_children_text(max_depth=2)}'+ msg = f'Clicked button with index {index_for_logging}: {element_node.get_all_children_text(max_depth=2)}'logger.info(f'🖱️ {msg}')logger.debug(f'Element xpath: {element_node.xpath}')@@ -81,7 +87,7 @@ class DefaultActionWatchdog(BaseWatchdog):BrowserErrorEvent(error_type='ClickFailed',message=str(e),- details={'index': event.index},+ details={'index': index_for_logging if 'index_for_logging' in locals() else event.index},))diff --git a/browser_use/browser/events.py b/browser_use/browser/events.pyindex 9f3e2d7ba..40870d701 100644--- a/browser_use/browser/events.py+++ b/browser_use/browser/events.py@@ -3,7 +3,7 @@from typing import Any, Literalfrom bubus import BaseEvent-from pydantic import Field+from pydantic import Field, model_validator# ============================================================================# Agent/Controller -> BrowserSession Events (High-level browser actions)@@ -20,14 +20,24 @@ class NavigateToUrlEvent(BaseEvent):class ClickElementEvent(BaseEvent):- """Click an element by index."""+ """Click an element by index or element_node."""- index: int+ index: int | None = None+ element_node: Any | None = None # DOMElementNode, but avoid circular importbutton: Literal['left', 'right', 'middle'] = 'left'click_count: int = 1expect_download: bool = Falsenew_tab: bool = False+ @model_validator(mode='after')+ def validate_index_or_element_node(self):+ """Validate that either index or element_node is provided."""+ if self.index is None and self.element_node is None:+ raise ValueError("Either 'index' or 'element_node' must be provided")+ if self.index is not None and self.element_node is not None:+ raise ValueError("Only one of 'index' or 'element_node' should be provided")+ return self+class TypeTextEvent(BaseEvent):"""Type text into an element."""diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 414bdf25b..a8c1e0678 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -29,7 +29,6 @@ from uuid_extensions import uuid7strfrom browser_use.browser.events import (ClickElementEvent,- CloseTabEvent,GoBackEvent,GoForwardEvent,NavigateToUrlEvent,@@ -50,7 +49,6 @@ from browser_use.browser.types import (FrameLocator,Page,Patchright,- Playwright,PlaywrightOrPatchright,async_patchright,async_playwright,@@ -2023,7 +2021,7 @@ class BrowserSession(BaseModel):self.logger.warning(f'⚠️ Page {_log_pretty_url(page.url)} failed to finish loading after click: {type(e).__name__}: {e}')- await self._check_and_handle_navigation(page)+ # Navigation is handled by NavigationWatchdog via eventselse:# If downloads are disabled, just perform the clickawait click_func()@@ -2033,7 +2031,7 @@ class BrowserSession(BaseModel):self.logger.warning(f'⚠️ Page {_log_pretty_url(page.url)} failed to finish loading after click: {type(e).__name__}: {e}')- await self._check_and_handle_navigation(page)+ # Navigation is handled by NavigationWatchdog via eventstry:return await perform_click(lambda: element_handle and element_handle.click(timeout=1_500))@@ -2061,19 +2059,19 @@ class BrowserSession(BaseModel):raise eexcept Exception as e:# Final fallback - try clicking by coordinates if available- if element_node.viewport_coordinates and element_node.viewport_coordinates.center:+ if element_node.absolute_position:try:+ center_x = element_node.absolute_position.x + element_node.absolute_position.width / 2+ center_y = element_node.absolute_position.y + element_node.absolute_position.height / 2self.logger.warning(- f'⚠️ Element click failed, falling back to coordinate click at ({element_node.viewport_coordinates.center.x}, {element_node.viewport_coordinates.center.y})'- )- await page.mouse.click(- element_node.viewport_coordinates.center.x, element_node.viewport_coordinates.center.y+ f'⚠️ Element click failed, falling back to coordinate click at ({center_x}, {center_y})')+ await page.mouse.click(center_x, center_y)try:await page.wait_for_load_state()except Exception:pass- await self._check_and_handle_navigation(page)+ # Navigation is handled by NavigationWatchdog via eventsreturn None # Successexcept Exception as coord_e:self.logger.error(f'Coordinate click also failed: {type(coord_e).__name__}: {coord_e}')@@ -2084,57 +2082,6 @@ class BrowserSession(BaseModel):except Exception as e:raise Exception(f'Failed to click element. Error: {str(e)}')- @time_execution_async('--get_tabs_info')- @retry(timeout=3, retries=1)- @require_healthy_browser(usable_page=False, reopen_page=False)- async def get_tabs_info(self) -> list[TabInfo]:- """Get information about all tabs"""- assert self.browser_context is not None, 'BrowserContext is not set up'- tabs_info = []- for page_id, page in enumerate(self.browser_context.pages):- # Skip JS execution for chrome:// pages and new tab pages- if is_new_tab_page(page.url) or page.url.startswith('chrome://'):- # Use URL as title for chrome pages, or mark new tabs as unusable- if is_new_tab_page(page.url):- tab_info = TabInfo(page_id=page_id, url=page.url, title='ignore this tab and do not use it')- else:- # For chrome:// pages, use the URL itself as the title- tab_info = TabInfo(page_id=page_id, url=page.url, title=page.url)- tabs_info.append(tab_info)- continue-- # Normal pages - try to get title with timeout- try:- title = await asyncio.wait_for(page.title(), timeout=2.0)- tab_info = TabInfo(page_id=page_id, url=page.url, title=title)- except Exception:- # page.title() can hang forever on tabs that are crashed/disappeared/about:blank- # but we should preserve the real URL and not mislead the LLM about tab availability- self.logger.debug(- f'⚠️ Failed to get tab info for tab #{page_id}: {_log_pretty_url(page.url)} (using fallback title)'- )-- # Only mark as unusable if it's actually a new tab page, otherwise preserve the real URL- if is_new_tab_page(page.url):- tab_info = TabInfo(page_id=page_id, url=page.url, title='ignore this tab and do not use it')- else:- # Preserve the real URL and use a descriptive fallback title- # fallback_title = '(title unavailable, page possibly crashed / unresponsive)'- # tab_info = TabInfo(page_id=page_id, url=page.url, title=fallback_title)-- # harsh but good, just close the page here because if we cant get the title then we certainly cant do anything else useful with it, no point keeping it open- try:- await page.close()- self.logger.debug(- f'🪓 Force-closed 🅟 {str(id(page))[-2:]} because its JS engine is unresponsive via CDP: {_log_pretty_url(page.url)}'- )- except Exception:- pass- continue-- tabs_info.append(tab_info)-- return tabs_info@retry(timeout=20, retries=1, semaphore_limit=1, semaphore_scope='self')async def _set_viewport_size(self, page: Page, viewport: dict[str, int] | ViewportSize) -> None:@@ -2331,7 +2278,6 @@ class BrowserSession(BaseModel):logger.warning(f'Failed to list downloaded files: {e}')return []-@propertydef pages(self) -> list[Page]:"""Get all open pages."""@@ -2575,8 +2521,7 @@ class BrowserSession(BaseModel):try:await self._wait_for_stable_network()- # Check if the loaded URL is allowed- await self._check_and_handle_navigation(page)+ # URL checking is handled by NavigationWatchdog via eventsexcept URLNotAllowedError as e:raise eexcept Exception as e:@@ -2615,12 +2560,6 @@ class BrowserSession(BaseModel):await eventreturn await self.get_current_page()- async def navigate(self, url: str, timeout_ms: int | None = None, new_tab: bool = False) -> Page:- """Navigate with optional timeout and new tab support."""- event = self.event_bus.dispatch(NavigateToUrlEvent(url=url, timeout_ms=timeout_ms, new_tab=new_tab))- await event- return await self.get_current_page()-async def go_to_url(self, url: str) -> None:"""Alias for navigate_to."""event = self.event_bus.dispatch(NavigateToUrlEvent(url=url))@@ -2679,7 +2618,6 @@ class BrowserSession(BaseModel):event = self.event_bus.dispatch(UploadFileEvent(element_index=element_index, file_path=file_path))await event-# Additional compatibility methodsasync def save_storage_state(self, path: str | None = None) -> None:@@ -2688,19 +2626,77 @@ class BrowserSession(BaseModel):self.event_bus.dispatch(SaveStorageStateEvent(path=path))async def get_tabs_info(self) -> list[TabInfo]:- """Get information about all open tabs."""+ """Get information about all open tabs using CDP for reliability."""tabs = []for i, page in enumerate(self.pages):- if not page.is_closed():- tab_info = TabInfo(- page_id=i,- url=page.url,- title=await page.title(),- parent_page_id=None,- id=f'tab_{i}',- index=i,- )- tabs.append(tab_info)+ if page.is_closed():+ continue++ # Skip JS execution for chrome:// pages and new tab pages+ if is_new_tab_page(page.url) or page.url.startswith('chrome://'):+ # Use URL as title for chrome pages, or mark new tabs as unusable+ if is_new_tab_page(page.url):+ title = 'ignore this tab and do not use it'+ else:+ # For chrome:// pages, use the URL itself as the title+ title = page.url+ else:+ # Normal pages - try to get title with CDP for reliability+ try:+ # Use CDP to get title with timeout+ cdp_client = await self.get_cdp_client()+ session_id = await self.get_current_page_cdp_session_id()++ # Use CDP to evaluate document.title+ title_result = await asyncio.wait_for(+ cdp_client.send.Runtime.evaluate(+ params={'expression': 'document.title'},+ session_id=session_id+ ),+ timeout=2.0+ )+ title = title_result.get('result', {}).get('value', '')++ # Special handling for PDF pages+ if (not title or title == '') and (page.url.endswith('.pdf') or 'pdf' in page.url):+ # PDF pages might not have a title, use URL filename+ try:+ from urllib.parse import urlparse+ filename = urlparse(page.url).path.split('/')[-1]+ if filename:+ title = filename+ except Exception:+ pass++ except Exception as e:+ # Page might be crashed or unresponsive+ self.logger.debug(+ f'⚠️ Failed to get tab info for tab #{i}: {_log_pretty_url(page.url)} - {type(e).__name__}'+ )++ # Only mark as unusable if it's actually a new tab page+ if is_new_tab_page(page.url):+ title = 'ignore this tab and do not use it'+ else:+ # For crashed pages, close them as they're not useful+ try:+ await page.close()+ self.logger.debug(+ f'🪓 Force-closed page because it\'s unresponsive: {_log_pretty_url(page.url)}'+ )+ continue+ except Exception:+ title = '(Error)'++ tab_info = TabInfo(+ page_id=i,+ url=page.url,+ title=title,+ parent_page_id=None,+ id=f'tab_{i}',+ index=i,+ )+ tabs.append(tab_info)return tabs# DOM element methods@@ -2753,7 +2749,8 @@ class BrowserSession(BaseModel):async def get_minimal_state_summary(self) -> BrowserStateSummary:"""Get basic page info without DOM processing, but try to capture screenshot"""from browser_use.browser.views import BrowserStateSummary- from browser_use.dom.views import EnhancedDOMTreeNode as DOMElementNode, NodeType, SerializedDOMState, DOMSelectorMap+ from browser_use.dom.views import EnhancedDOMTreeNode as DOMElementNode+ from browser_use.dom.views import NodeType, SerializedDOMStatepage = await self.get_current_page()@@ -2803,7 +2800,7 @@ class BrowserSession(BaseModel):_root=None, # No simplified tree for minimal stateselector_map={}, # Empty selector map)-+return BrowserStateSummary(dom_state=minimal_dom_state,url=url,@@ -2889,14 +2886,10 @@ class BrowserSession(BaseModel):self.logger.debug('🌳 Starting DOM processing...')from browser_use.dom.service import DomService- dom_service = DomService(page, logger=self.logger)+ dom_service = DomService(self, page, logger=self.logger)try:- content = await asyncio.wait_for(- dom_service.get_clickable_elements(- focus_element=focus_element,- viewport_expansion=self.browser_profile.viewport_expansion,- highlight_elements=self.browser_profile.highlight_elements,- ),+ content, _ = await asyncio.wait_for(+ dom_service.get_serialized_dom_tree(),timeout=45.0, # 45 second timeout for DOM processing - generous for complex pages)self.logger.debug('✅ DOM processing completed')@@ -2905,20 +2898,12 @@ class BrowserSession(BaseModel):self.logger.warning('🔄 Falling back to minimal DOM state to allow basic navigation...')# Create minimal DOM state for basic navigation- from browser_use.dom.views import EnhancedDOMTreeNode as DOMElementNode-- minimal_element_tree = DOMElementNode(- tag_name='body',- xpath='/body',- attributes={},- children=[],- is_visible=True,- parent=None,- )+ from browser_use.dom.views import SerializedDOMState- from browser_use.dom.views import DOMState-- content = DOMState(element_tree=minimal_element_tree, selector_map={})+ content = SerializedDOMState(+ _root=None, # No simplified tree for minimal state+ selector_map={}, # Empty selector map+ )self.logger.debug('📋 Getting tabs info...')tabs_info = await self.get_tabs_info()@@ -2983,8 +2968,7 @@ class BrowserSession(BaseModel):is_pdf_viewer = await self._is_pdf_viewer(page)self.browser_state_summary = BrowserStateSummary(- element_tree=content.element_tree,- selector_map=content.selector_map,+ dom_state=content,url=page.url,title=title,tabs=tabs_info,@@ -2994,7 +2978,6 @@ class BrowserSession(BaseModel):pixels_below=pixels_below,browser_errors=browser_errors,is_pdf_viewer=is_pdf_viewer,- loading_status=self._current_page_loading_status,)self.logger.debug('✅ get_state_summary completed successfully')@@ -3445,7 +3428,7 @@ class BrowserSession(BaseModel):except Exception:# Fallback to a more basic selector if something goes wrongtag_name = element.tag_name or '*'- return f"{tag_name}"+ return f'{tag_name}'@require_healthy_browser(usable_page=True, reopen_page=True)@time_execution_async('--is_visible')@@ -3548,7 +3531,7 @@ class BrowserSession(BaseModel):async def _click_element_node_compat(self, element: Any, expect_download: bool = False, new_tab: bool = False) -> str | None:"""Click an element node and handle potential downloads.-+DEPRECATED: This is a compatibility method. Use the event-based approach instead.Args:@@ -3562,7 +3545,9 @@ class BrowserSession(BaseModel):raise ValueError('No element provided to click')# This method is deprecated - clicking is now handled by DefaultActionWatchdog- raise NotImplementedError("_click_element_node is deprecated. Use click_element() which dispatches ClickElementEvent instead.")+ raise NotImplementedError(+ '_click_element_node is deprecated. Use click_element() which dispatches ClickElementEvent instead.'+ )@require_healthy_browser(usable_page=True, reopen_page=True)@time_execution_async('--input_text_element_node')@@ -4084,7 +4069,7 @@ class BrowserSession(BaseModel):Check if the current page is a PDF viewer."""try:- is_pdf = await page.evaluate("window.isPdfViewer || false")+ is_pdf = await page.evaluate('window.isPdfViewer || false')return bool(is_pdf)except Exception:return Falsediff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 5544c8fe2..26c27bcca 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -15,6 +15,7 @@ from pydantic import BaseModelfrom browser_use.agent.views import ActionModel, ActionResultfrom browser_use.browser import BrowserSession+from browser_use.browser.events import ClickElementEventfrom browser_use.browser.types import Pagefrom browser_use.browser.views import BrowserErrorfrom browser_use.controller.registry.service import Registry