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
prevent agent from clicking elements with index 0
Nick Sweeting8937b4f09454
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex c2144b5c0..1965ff771 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -283,17 +283,6 @@ class BrowserSession(BaseModel):'This likely means BrowserSession was initialized multiple times with the same EventBus.')- # Register BrowserSession's event handlers- # self.event_bus.on(BrowserStartEvent, self.on_BrowserStartEvent)- # self.event_bus.on(BrowserStopEvent, self.on_BrowserStopEvent)- # # Core navigation events that shouldn't be in optional watchdogs- # self.event_bus.on(NavigateToUrlEvent, self.on_NavigateToUrlEvent)- # self.event_bus.on(SwitchTabEvent, self.on_SwitchTabEvent)- # self.event_bus.on(TabClosedEvent, self.on_TabClosedEvent)- # self.event_bus.on(AgentFocusChangedEvent, self.on_AgentFocusChangedEvent)- # # Track downloaded files- # self.event_bus.on(FileDownloadedEvent, self.on_FileDownloadedEvent)-BaseWatchdog.attach_handler_to_session(self, BrowserStartEvent, self.on_BrowserStartEvent)BaseWatchdog.attach_handler_to_session(self, BrowserStopEvent, self.on_BrowserStopEvent)BaseWatchdog.attach_handler_to_session(self, NavigateToUrlEvent, self.on_NavigateToUrlEvent)diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 6e6f4dd72..548a79e0f 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -239,13 +239,15 @@ class Controller(Generic[Context]):param_model=ClickElementAction,)async def click_element_by_index(params: ClickElementAction, browser_session: BrowserSession):- # Look up the node from the selector map- node = await browser_session.get_element_by_index(params.index)- if node is None:- raise ValueError(f'Element index {params.index} not found in DOM')-# 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'++ # Look up the node from the selector map+ node = await browser_session.get_element_by_index(params.index)+ if node is None:+ raise ValueError(f'Element index {params.index} not found in DOM')+event = browser_session.event_bus.dispatch(ClickElementEvent(node=node, new_tab=params.new_tab))await event# Wait for handler to complete and get any exception (None is expected on success)diff --git a/browser_use/controller/views.py b/browser_use/controller/views.pyindex 4935b14f0..5d7be1671 100644--- a/browser_use/controller/views.py+++ b/browser_use/controller/views.py@@ -14,7 +14,7 @@ class GoToUrlAction(BaseModel):class ClickElementAction(BaseModel):- index: int+ index: int = Field(ge=1, description='index of the element to click')new_tab: bool = Field(default=False, description='set True to open any resulting navigation in a new tab, False otherwise')# expect_download: bool = Field(default=False, description='set True if expecting a download, False otherwise') # moved to downloads_watchdog.py# click_count: int = 1 # TODO