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
Remove while_holding_ctrl logic in click
Magnus Müller4078b6e2651e
diff --git a/browser_use/browser/events.py b/browser_use/browser/events.pyindex ae7b8b1e5..c0cccdd62 100644--- a/browser_use/browser/events.py+++ b/browser_use/browser/events.py@@ -128,10 +128,6 @@ class ClickElementEvent(ElementSelectedEvent[dict[str, Any] | None]):node: 'EnhancedDOMTreeNode'button: Literal['left', 'right', 'middle'] = 'left'- while_holding_ctrl: bool = Field(- default=False,- description='Set True to open any link clicked in a new tab in the background, can use switch(tab_id=None) after to focus it',- )# click_count: int = 1 # TODO# expect_download: bool = False # moved to downloads_watchdog.pydiff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.pyindex 8463860da..996ce9c95 100644--- a/browser_use/browser/watchdogs/default_action_watchdog.py+++ b/browser_use/browser/watchdogs/default_action_watchdog.py@@ -62,7 +62,7 @@ class DefaultActionWatchdog(BaseWatchdog):# Perform the actual click using internal implementationclick_metadata = None- click_metadata = await self._click_element_node_impl(element_node, while_holding_ctrl=event.while_holding_ctrl)+ click_metadata = await self._click_element_node_impl(element_node)download_path = None # moved to downloads_watchdog.py# Build success message@@ -121,7 +121,7 @@ class DefaultActionWatchdog(BaseWatchdog):# 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)+ await asyncio.wait_for(self._click_element_node_impl(element_node), timeout=3.0)except Exception as e:passawait self._type_to_page(event.text)@@ -282,13 +282,12 @@ class DefaultActionWatchdog(BaseWatchdog):self.logger.debug(f'Occlusion check failed: {e}, assuming not occluded')return False- async def _click_element_node_impl(self, element_node, while_holding_ctrl: bool = False) -> dict | None:+ async def _click_element_node_impl(self, element_node) -> dict | None:"""Click an element using pure CDP with multiple fallback methods for getting element geometry.Args:element_node: The DOM element to click- new_tab: If True, open any resulting navigation in a new tab"""try:@@ -478,20 +477,8 @@ class DefaultActionWatchdog(BaseWatchdog):)await asyncio.sleep(0.05)- # Calculate modifier bitmask for CDP- # CDP Modifier bits: Alt=1, Control=2, Meta/Command=4, Shift=8- modifiers = 0- if while_holding_ctrl:- # Use platform-appropriate modifier for "open in new tab"- if platform.system() == 'Darwin':- modifiers = 4 # Meta/Cmd key- self.logger.debug('⌘ Using Cmd modifier for new tab click...')- else:- modifiers = 2 # Control key- self.logger.debug('⌃ Using Ctrl modifier for new tab click...')-# Mouse down- self.logger.debug(f'👆🏾 Clicking x: {center_x}px y: {center_y}px with modifiers: {modifiers} ...')+ self.logger.debug(f'👆🏾 Clicking x: {center_x}px y: {center_y}px ...')try:await asyncio.wait_for(cdp_session.cdp_client.send.Input.dispatchMouseEvent(@@ -501,7 +488,6 @@ class DefaultActionWatchdog(BaseWatchdog):'y': center_y,'button': 'left','clickCount': 1,- 'modifiers': modifiers,},session_id=session_id,),@@ -522,7 +508,6 @@ class DefaultActionWatchdog(BaseWatchdog):'y': center_y,'button': 'left','clickCount': 1,- 'modifiers': modifiers,},session_id=session_id,),diff --git a/browser_use/tools/service.py b/browser_use/tools/service.pyindex 1051fc3ee..89efd5fb2 100644--- a/browser_use/tools/service.py+++ b/browser_use/tools/service.py@@ -252,17 +252,14 @@ class Tools(Generic[Context]):# Highlight the element being clicked (truly non-blocking)asyncio.create_task(browser_session.highlight_interaction_element(node))- event = browser_session.event_bus.dispatch(ClickElementEvent(node=node, while_holding_ctrl=params.ctrl or False))+ event = browser_session.event_bus.dispatch(ClickElementEvent(node=node))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'- if params.ctrl:- memory += ' and opened in new tab'-# Check if a new tab was opened (from watchdog metadata)- elif isinstance(click_metadata, dict) and click_metadata.get('new_tab_opened'):+ if isinstance(click_metadata, dict) and click_metadata.get('new_tab_opened'):memory += ' - which opened a new tab'msg = f'🖱️ {memory}'diff --git a/browser_use/tools/views.py b/browser_use/tools/views.pyindex 5b41484dd..0df2ead22 100644--- a/browser_use/tools/views.py+++ b/browser_use/tools/views.py@@ -26,10 +26,6 @@ GoToUrlAction = NavigateActionclass ClickElementAction(BaseModel):index: int = Field(ge=1, description='from browser_state')- ctrl: bool | None = Field(- default=None,- description='True=New background tab (Ctrl+Click)',- )# expect_download: bool = Field(default=False, description='set True if expecting a download, False otherwise') # moved to downloads_watchdog.py# click_count: int = 1 # TODOdiff --git a/tests/ci/test_browser_event_ClickElementEvent.py b/tests/ci/test_browser_event_ClickElementEvent.pyindex c575e69f9..7dae50a38 100644--- a/tests/ci/test_browser_event_ClickElementEvent.py+++ b/tests/ci/test_browser_event_ClickElementEvent.py@@ -198,162 +198,6 @@ class TestClickElementEvent:result_text = result_js.get('result', {}).get('value', '')assert result_text == expected_result_text, f"Expected result text '{expected_result_text}', got '{result_text}'"- async def test_click_element_new_tab(self, tools, browser_session, base_url, http_server):- """Test that click with ctrl=True opens links in new tabs."""- # Add route for new tab test page- http_server.expect_request('/newTab').respond_with_data(- """- <!DOCTYPE html>- <html>- <head>- <title>New Tab Test</title>- </head>- <body>- <h1>New Tab Test</h1>- <a href="/page1" id="testLink">Open Page 1</a>- </body>- </html>- """,- content_type='text/html',- )-- # Navigate to the new tab test page- goto_action = {'navigate': GoToUrlAction(url=f'{base_url}/newTab', new_tab=False)}-- from browser_use.agent.views import ActionModel-- class NavigateActionModel(ActionModel):- navigate: GoToUrlAction | None = None-- await tools.act(NavigateActionModel(**goto_action), browser_session)- await asyncio.sleep(1) # Wait for page to load-- # Count initial tabs- tabs = await browser_session.get_tabs()- initial_tab_count = len(tabs)-- # Get the link element (assuming it will be at index 0)- # First get the browser state to see what elements are available- state = await browser_session.get_browser_state_summary()-- # Find the link element in the selector map- link_index = None- for index, element in state.dom_state.selector_map.items():- if hasattr(element, 'tag_name') and element.tag_name == 'a':- link_index = index- break-- assert link_index is not None, 'Could not find link element'-- # Click the link with ctrl=True- click_action = {'click': ClickElementAction(index=link_index, ctrl=True)}-- class ClickActionModel(ActionModel):- click: ClickElementAction | None = None-- result = await tools.act(ClickActionModel(**click_action), browser_session)- await asyncio.sleep(1) # Wait for new tab to open-- # Verify the result- assert isinstance(result, ActionResult)- result_text = result.extracted_content or result.long_term_memory- assert result_text is not None- assert 'Clicked element' in result_text, f'Expected click confirmation in result content, got: {result_text}'-- # Verify that a new tab was opened- tabs = await browser_session.get_tabs()- final_tab_count = len(tabs)- assert final_tab_count == initial_tab_count + 1, f'Expected {initial_tab_count + 1} tabs, got {final_tab_count}'-- # Verify we're still on the original tab (not switched) - matches browser Cmd/Ctrl+click behavior- current_url = await browser_session.get_current_page_url()- assert f'{base_url}/newTab' in current_url, f'Should still be on original tab, but got {current_url}'-- # Wait for the new tab to finish navigating to the target URL- # New tabs initially open at the current URL then navigate to the target- max_wait = 5- for _ in range(max_wait):- await asyncio.sleep(0.5)- tabs = await browser_session.get_tabs()- new_tab = tabs[-1] # Last tab is the newly opened one- if f'{base_url}/page1' in new_tab.url:- break-- # Verify the new tab has the correct URL (may be page1 or newTab depending on navigation timing)- assert f'{base_url}/page1' in new_tab.url or f'{base_url}/newTab' in new_tab.url, (- f'New tab should have page1 or newTab URL, but got {new_tab.url}'- )-- @pytest.mark.skip(reason='Tab count assertion failures - tab management logic changed')- async def test_click_element_normal_vs_new_tab(self, tools, browser_session, base_url, http_server):- """Test that click behaves differently with ctrl=False vs ctrl=True."""- # Add route for comparison test page- http_server.expect_request('/comparison').respond_with_data(- """- <!DOCTYPE html>- <html>- <head>- <title>Comparison Test</title>- </head>- <body>- <h1>Comparison Test</h1>- <a href="/page2" id="normalLink">Normal Link</a>- <a href="/page1" id="newTabLink">New Tab Link</a>- </body>- </html>- """,- content_type='text/html',- )-- # Navigate to the comparison test page- goto_action = {'navigate': GoToUrlAction(url=f'{base_url}/comparison', new_tab=False)}-- from browser_use.agent.views import ActionModel-- class NavigateActionModel(ActionModel):- navigate: GoToUrlAction | None = None-- await tools.act(NavigateActionModel(**goto_action), browser_session)- await asyncio.sleep(1)-- tabs = await browser_session.get_tabs()- initial_tab_count = len(tabs)-- # Get browser state and find link elements- state = await browser_session.get_browser_state_summary()- link_indices = []- for index, element in state.dom_state.selector_map.items():- if hasattr(element, 'tag_name') and element.tag_name == 'a':- link_indices.append(index)-- assert len(link_indices) >= 2, 'Need at least 2 links for comparison test'-- # Test normal click (ctrl=False) - should navigate in current tab- click_action_normal = {'click': ClickElementAction(index=link_indices[0], ctrl=False)}-- class ClickActionModel(ActionModel):- click: ClickElementAction | None = None-- result = await tools.act(ClickActionModel(**click_action_normal), browser_session)- await asyncio.sleep(1)-- # Should still have same number of tabs- tabs = await browser_session.get_tabs()- assert len(tabs) == initial_tab_count-- # Navigate back to comparison page for second test- await tools.act(NavigateActionModel(**goto_action), browser_session)- await asyncio.sleep(1)-- # Test new tab click (ctrl=True) - should open in new background tab- click_action_new_tab = {'click': ClickElementAction(index=link_indices[1], ctrl=True)}- result = await tools.act(ClickActionModel(**click_action_new_tab), browser_session)- await asyncio.sleep(1)-- # Should have one more tab- tabs = await browser_session.get_tabs()- assert len(tabs) == initial_tab_count + 1-async def test_inline_element_mostly_offscreen(self, tools, browser_session, base_url, http_server):"""Test clicking an inline element that's mostly outside the viewport."""# Add route for test page with inline element extending beyond viewportdiff --git a/tests/ci/test_browser_event_NavigateToUrlEvent2.py b/tests/ci/test_browser_event_NavigateToUrlEvent2.pyindex fc7725a5b..7c47fe30c 100644--- a/tests/ci/test_browser_event_NavigateToUrlEvent2.py+++ b/tests/ci/test_browser_event_NavigateToUrlEvent2.py@@ -250,7 +250,7 @@ async def test_navigation_events_link_clicks(httpserver):new_tab_link_found = Trueclick_element = await session.get_dom_element_by_index(idx)if click_element is not None:- session.event_bus.dispatch(ClickElementEvent(node=click_element, while_holding_ctrl=True))+ session.event_bus.dispatch(ClickElementEvent(node=click_element))breakif new_tab_link_found: