Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix: make actor input operations match browser actions

MagMueller35d65335b213
diff --git a/browser_use/actor/element.py b/browser_use/actor/element.py
index cc5b84d2a..562a88fdb 100644
--- a/browser_use/actor/element.py
+++ b/browser_use/actor/element.py
@@ -351,160 +351,58 @@ class Element:
raise RuntimeError(f'Failed to click element: {e}')
async def fill(self, value: str, clear: bool = True) -> None:
- """Fill the input element using proper CDP methods with improved focus handling."""
- try:
- # Use the existing CDP client and session
- cdp_client = self._client
- session_id = self._session_id
- backend_node_id = self._backend_node_id
-
- # Track coordinates for metadata
- input_coordinates = None
-
- # Scroll element into view
- try:
- await cdp_client.send.DOM.scrollIntoViewIfNeeded(params={'backendNodeId': backend_node_id}, session_id=session_id)
- await asyncio.sleep(0.01)
- except Exception as e:
- logger.warning(f'Failed to scroll element into view: {e}')
-
- # Get object ID for the element
- result = await cdp_client.send.DOM.resolveNode(
- params={'backendNodeId': backend_node_id},
- session_id=session_id,
- )
- if 'object' not in result or 'objectId' not in result['object']:
- raise RuntimeError('Failed to get object ID for element')
- object_id = result['object']['objectId']
-
- # Get element coordinates for focus
- try:
- bounds_result = await cdp_client.send.Runtime.callFunctionOn(
- params={
- 'functionDeclaration': 'function() { return this.getBoundingClientRect(); }',
- 'objectId': object_id,
- 'returnByValue': True,
- },
- session_id=session_id,
- )
- if bounds_result.get('result', {}).get('value'):
- bounds = bounds_result['result']['value'] # type: ignore
- center_x = bounds['x'] + bounds['width'] / 2
- center_y = bounds['y'] + bounds['height'] / 2
- input_coordinates = {'input_x': center_x, 'input_y': center_y}
- logger.debug(f'Using element coordinates: x={center_x:.1f}, y={center_y:.1f}')
- except Exception as e:
- logger.debug(f'Could not get element coordinates: {e}')
-
- # Ensure session_id is not None
- if session_id is None:
- raise RuntimeError('Session ID is required for fill operation')
+ """Fill through the normal text action, including native date/time inputs."""
+ from browser_use.browser.events import TypeTextEvent
+ from browser_use.dom.views import EnhancedDOMTreeNode, NodeType
- # Step 1: Focus the element
- focused_successfully = await self._focus_element_simple(
- backend_node_id=backend_node_id,
- object_id=object_id,
- cdp_client=cdp_client,
- session_id=session_id,
- input_coordinates=input_coordinates,
- )
-
- # Step 2: Clear existing text if requested
- if clear:
- cleared_successfully = await self._clear_text_field(
- object_id=object_id, cdp_client=cdp_client, session_id=session_id
- )
- if not cleared_successfully:
- logger.warning('Text field clearing failed, typing may append to existing text')
-
- # Step 3: Type the text character by character using proper human-like key events
- logger.debug(f'Typing text character by character: "[REDACTED {len(value)} chars]"')
-
- for i, char in enumerate(value):
- # Handle newline characters as Enter key
- if char == '\n':
- # Send proper Enter key sequence
- await cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'keyDown',
- 'key': 'Enter',
- 'code': 'Enter',
- 'windowsVirtualKeyCode': 13,
- },
- session_id=session_id,
- )
-
- # Small delay to emulate human typing speed
- await asyncio.sleep(0.001)
-
- # Send char event with carriage return
- await cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'char',
- 'text': '\r',
- 'key': 'Enter',
- },
- session_id=session_id,
- )
-
- # Send keyUp event
- await cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'keyUp',
- 'key': 'Enter',
- 'code': 'Enter',
- 'windowsVirtualKeyCode': 13,
- },
- session_id=session_id,
- )
- else:
- # Handle regular characters
- # Get proper modifiers, VK code, and base key for the character
- modifiers, vk_code, base_key = self._get_char_modifiers_and_vk(char)
- key_code = self._get_key_code_for_char(base_key)
-
- # Step 1: Send keyDown event (NO text parameter)
- await cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'keyDown',
- 'key': base_key,
- 'code': key_code,
- 'modifiers': modifiers,
- 'windowsVirtualKeyCode': vk_code,
- },
- session_id=session_id,
- )
-
- # Small delay to emulate human typing speed
- await asyncio.sleep(0.001)
-
- # Step 2: Send char event (WITH text parameter) - this is crucial for text input
- await cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'char',
- 'text': char,
- 'key': char,
- },
- session_id=session_id,
- )
-
- # Step 3: Send keyUp event (NO text parameter)
- await cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'keyUp',
- 'key': base_key,
- 'code': key_code,
- 'modifiers': modifiers,
- 'windowsVirtualKeyCode': vk_code,
- },
- session_id=session_id,
- )
+ description = await self._client.send.DOM.describeNode(
+ params={'backendNodeId': self._backend_node_id},
+ session_id=self._session_id,
+ )
+ node = description['node']
+ info = await self._client.send.Target.getTargetInfo(session_id=self._session_id)
+ attributes = node.get('attributes', [])
+ element = EnhancedDOMTreeNode(
+ node_id=node['nodeId'],
+ backend_node_id=self._backend_node_id,
+ node_type=NodeType(node['nodeType']),
+ node_name=node['nodeName'],
+ node_value=node['nodeValue'],
+ attributes=dict(zip(attributes[::2], attributes[1::2])),
+ is_scrollable=None,
+ is_visible=None,
+ absolute_position=None,
+ target_id=info['targetInfo']['targetId'],
+ frame_id=node.get('frameId'),
+ session_id=self._session_id,
+ content_document=None,
+ shadow_root_type=None,
+ shadow_roots=None,
+ parent_node=None,
+ children_nodes=None,
+ ax_node=None,
+ snapshot_node=None,
+ )
+ event = self._browser_session.event_bus.dispatch(TypeTextEvent(node=element, text=value, clear=clear, is_sensitive=True))
+ await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+
+ async def scroll_into_view(self) -> None:
+ """Scroll the element into the viewport using its backend node ID."""
+ await self._client.send.DOM.scrollIntoViewIfNeeded(
+ params={'backendNodeId': self._backend_node_id},
+ session_id=self._session_id,
+ )
- # Add 18ms delay between keystrokes
- await asyncio.sleep(0.018)
+ async def set_input_files(self, paths: list[str]) -> None:
+ """Set a file input to paths on the browser host; an empty list clears it.
- except Exception as e:
- raise Exception(f'Failed to fill element: {str(e)}')
+ Remote browsers require files to be staged on that host first.
+ """
+ await self._client.send.DOM.setFileInputFiles(
+ params={'files': paths, 'backendNodeId': self._backend_node_id},
+ session_id=self._session_id,
+ )
async def hover(self) -> None:
"""Hover over the element."""
@@ -621,21 +519,16 @@ class Element:
target_x = target_box['x'] + target_box['width'] / 2
target_y = target_box['y'] + target_box['height'] / 2
- # Perform drag operation
- await self._client.send.Input.dispatchMouseEvent(
- {'type': 'mousePressed', 'x': source_x, 'y': source_y, 'button': 'left'},
- session_id=self._session_id,
- )
-
- await self._client.send.Input.dispatchMouseEvent(
- {'type': 'mouseMoved', 'x': target_x, 'y': target_y},
- session_id=self._session_id,
- )
+ # Reuse one Mouse instance so movement carries the held-button mask.
+ from browser_use.actor.mouse import Mouse
- await self._client.send.Input.dispatchMouseEvent(
- {'type': 'mouseReleased', 'x': target_x, 'y': target_y, 'button': 'left'},
- session_id=self._session_id,
- )
+ mouse = Mouse(self._browser_session, self._session_id)
+ await mouse.move(source_x, source_y)
+ try:
+ await mouse.down()
+ await mouse.move(target_x, target_y, steps=10)
+ finally:
+ await mouse.up()
# Element properties and queries
async def get_attribute(self, name: str) -> str | None:
diff --git a/browser_use/actor/mouse.py b/browser_use/actor/mouse.py
index e78ee7124..4dee642ae 100644
--- a/browser_use/actor/mouse.py
+++ b/browser_use/actor/mouse.py
@@ -1,6 +1,6 @@
"""Mouse class for mouse operations."""
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Literal
if TYPE_CHECKING:
from cdp_use.cdp.input.commands import DispatchMouseEventParameters, SynthesizeScrollGestureParameters
@@ -28,70 +28,100 @@ class Mouse:
self._client = browser_session.cdp_client
self._session_id = session_id
self._target_id = target_id
-
- async def click(self, x: int, y: int, button: 'MouseButton' = 'left', click_count: int = 1) -> None:
- """Click at the specified coordinates."""
- # Mouse press
- press_params: 'DispatchMouseEventParameters' = {
- 'type': 'mousePressed',
- 'x': x,
- 'y': y,
- 'button': button,
- 'clickCount': click_count,
- }
- await self._client.send.Input.dispatchMouseEvent(
- press_params,
- session_id=self._session_id,
- )
-
- # Mouse release
- release_params: 'DispatchMouseEventParameters' = {
- 'type': 'mouseReleased',
- 'x': x,
- 'y': y,
- 'button': button,
- 'clickCount': click_count,
- }
- await self._client.send.Input.dispatchMouseEvent(
- release_params,
- session_id=self._session_id,
- )
-
- async def down(self, button: 'MouseButton' = 'left', click_count: int = 1) -> None:
- """Press mouse button down."""
+ self._x: float = 0
+ self._y: float = 0
+ self._buttons = 0
+
+ @staticmethod
+ def _modifiers(modifiers: list[Literal['Alt', 'Control', 'Meta', 'Shift']] | None) -> int:
+ bits = {'Alt': 1, 'Control': 2, 'Meta': 4, 'Shift': 8}
+ return sum(bits[key] for key in set(modifiers or []))
+
+ @staticmethod
+ def _button_bit(button: 'MouseButton') -> int:
+ return {'none': 0, 'left': 1, 'right': 2, 'middle': 4, 'back': 8, 'forward': 16}[button]
+
+ async def click(
+ self,
+ x: float,
+ y: float,
+ button: 'MouseButton' = 'left',
+ click_count: int = 1,
+ modifiers: list[Literal['Alt', 'Control', 'Meta', 'Shift']] | None = None,
+ ) -> None:
+ """Click at viewport coordinates, emitting complete click sequences."""
+ if click_count < 1:
+ raise ValueError('click_count must be positive')
+ await self.move(x, y, modifiers=modifiers)
+ for count in range(1, click_count + 1):
+ try:
+ await self.down(button, count, modifiers=modifiers)
+ finally:
+ await self.up(button, count, modifiers=modifiers)
+
+ async def down(
+ self,
+ button: 'MouseButton' = 'left',
+ click_count: int = 1,
+ modifiers: list[Literal['Alt', 'Control', 'Meta', 'Shift']] | None = None,
+ ) -> None:
+ """Press at this Mouse instance's last position and retain button state."""
+ buttons = self._buttons | self._button_bit(button)
params: 'DispatchMouseEventParameters' = {
'type': 'mousePressed',
- 'x': 0, # Will use last mouse position
- 'y': 0,
+ 'x': self._x,
+ 'y': self._y,
'button': button,
+ 'buttons': buttons,
'clickCount': click_count,
+ 'modifiers': self._modifiers(modifiers),
}
- await self._client.send.Input.dispatchMouseEvent(
- params,
- session_id=self._session_id,
- )
+ await self._client.send.Input.dispatchMouseEvent(params, session_id=self._session_id)
+ self._buttons = buttons
- async def up(self, button: 'MouseButton' = 'left', click_count: int = 1) -> None:
- """Release mouse button."""
+ async def up(
+ self,
+ button: 'MouseButton' = 'left',
+ click_count: int = 1,
+ modifiers: list[Literal['Alt', 'Control', 'Meta', 'Shift']] | None = None,
+ ) -> None:
+ """Release at the last position, preserving any other held buttons."""
+ buttons = self._buttons & ~self._button_bit(button)
params: 'DispatchMouseEventParameters' = {
'type': 'mouseReleased',
- 'x': 0, # Will use last mouse position
- 'y': 0,
+ 'x': self._x,
+ 'y': self._y,
'button': button,
+ 'buttons': buttons,
'clickCount': click_count,
+ 'modifiers': self._modifiers(modifiers),
}
- await self._client.send.Input.dispatchMouseEvent(
- params,
- session_id=self._session_id,
- )
-
- async def move(self, x: int, y: int, steps: int = 1) -> None:
- """Move mouse to the specified coordinates."""
- # TODO: Implement smooth movement with multiple steps if needed
- _ = steps # Acknowledge parameter for future use
-
- params: 'DispatchMouseEventParameters' = {'type': 'mouseMoved', 'x': x, 'y': y}
await self._client.send.Input.dispatchMouseEvent(params, session_id=self._session_id)
+ self._buttons = buttons
+
+ async def move(
+ self,
+ x: float,
+ y: float,
+ steps: int = 1,
+ modifiers: list[Literal['Alt', 'Control', 'Meta', 'Shift']] | None = None,
+ ) -> None:
+ """Move in linear steps while retaining pressed buttons for dragging."""
+ if steps < 1:
+ raise ValueError('steps must be positive')
+ start_x, start_y = self._x, self._y
+ for step in range(1, steps + 1):
+ px = start_x + (x - start_x) * step / steps
+ py = start_y + (y - start_y) * step / steps
+ params: 'DispatchMouseEventParameters' = {
+ 'type': 'mouseMoved',
+ 'x': px,
+ 'y': py,
+ 'buttons': self._buttons,
+ 'modifiers': self._modifiers(modifiers),
+ }
+ await self._client.send.Input.dispatchMouseEvent(params, session_id=self._session_id)
+ self._x, self._y = px, py
async def scroll(
self, x: int | None = None, y: int | None = None, delta_x: int | None = None, delta_y: int | None = None
@@ -136,8 +166,8 @@ class Mouse:
params: 'SynthesizeScrollGestureParameters' = {
'x': scroll_x,
'y': scroll_y,
- 'xDistance': delta_x or 0,
- 'yDistance': delta_y or 0,
+ 'xDistance': -(delta_x or 0),
+ 'yDistance': -(delta_y or 0),
}
await self._client.send.Input.synthesizeScrollGesture(
params,
diff --git a/browser_use/actor/page.py b/browser_use/actor/page.py
index ff210de2c..2476b37a9 100644
--- a/browser_use/actor/page.py
+++ b/browser_use/actor/page.py
@@ -1,5 +1,7 @@
"""Page class for page-level operations."""
+import asyncio
+import math
from typing import TYPE_CHECKING, TypeVar
from pydantic import BaseModel
@@ -22,6 +24,7 @@ if TYPE_CHECKING:
DispatchKeyEventParameters,
)
from cdp_use.cdp.page.commands import CaptureScreenshotParameters, NavigateParameters, NavigateToHistoryEntryParameters
+ from cdp_use.cdp.page.types import Viewport
from cdp_use.cdp.runtime.commands import EvaluateParameters
from cdp_use.cdp.target.commands import (
AttachToTargetParameters,
@@ -189,12 +192,13 @@ class Page:
return js_code
- async def screenshot(self, format: str = 'png', quality: int | None = None) -> str:
+ async def screenshot(self, format: str = 'png', quality: int | None = None, *, clip: 'Viewport | None' = None) -> str:
"""Take a screenshot and return base64 encoded image.
Args:
format: Image format ('jpeg', 'png', 'webp')
quality: Quality 0-100 for JPEG format
+ clip: Optional document-pixel region and output scale for a crop/zoom.
Returns:
Base64-encoded image data
@@ -202,6 +206,10 @@ class Page:
session_id = await self._ensure_session()
params: 'CaptureScreenshotParameters' = {'format': format}
+ if clip is not None:
+ if clip['width'] <= 0 or clip['height'] <= 0 or clip['scale'] <= 0:
+ raise ValueError('Screenshot clip width, height and scale must be positive')
+ params['clip'] = clip
if quality is not None and format.lower() == 'jpeg':
params['quality'] = quality
@@ -211,70 +219,60 @@ class Page:
return result['data']
async def press(self, key: str) -> None:
- """Press a key on the page (sends keyboard input to the focused element or page)."""
- session_id = await self._ensure_session()
+ """Press a key/chord or type literal text using the normal keyboard action."""
+ from browser_use.browser.events import SendKeysEvent
+
+ event = self._browser_session.event_bus.dispatch(SendKeysEvent(keys=key, target_id=self._target_id))
+ await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+
+ async def hold_key(self, key: str, duration: float) -> None:
+ """Hold a key/chord for seconds, releasing every pressed key on exit.
- # Handle key combinations like "Control+A"
- if '+' in key:
- parts = key.split('+')
- modifiers = parts[:-1]
- main_key = parts[-1]
-
- # Calculate modifier bitmask
- modifier_value = 0
- modifier_map = {'Alt': 1, 'Control': 2, 'Meta': 4, 'Shift': 8}
- for mod in modifiers:
- modifier_value |= modifier_map.get(mod, 0)
-
- # Press modifier keys
- for mod in modifiers:
- code, vk_code = get_key_info(mod)
- params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': mod, 'code': code}
+ This emits one key-down per key; it does not synthesize auto-repeat.
+ """
+ if not math.isfinite(duration) or duration < 0:
+ raise ValueError('duration must be finite and non-negative')
+ aliases = {
+ 'ctrl': 'Control',
+ 'control': 'Control',
+ 'alt': 'Alt',
+ 'cmd': 'Meta',
+ 'meta': 'Meta',
+ 'shift': 'Shift',
+ 'enter': 'Enter',
+ 'return': 'Enter',
+ 'tab': 'Tab',
+ 'escape': 'Escape',
+ 'space': ' ',
+ }
+ keys = [aliases.get(part.lower(), part) for part in key.split('+')] if key != '+' else ['+']
+ modifier_bits = {'Alt': 1, 'Control': 2, 'Meta': 4, 'Shift': 8}
+ if not keys or any(not part for part in keys) or any(part not in modifier_bits for part in keys[:-1]):
+ raise ValueError('Use one key or a modifier chord, such as Control+a')
+ session_id = await self._ensure_session()
+ held: list[str] = []
+ modifiers = 0
+ try:
+ for part in keys:
+ code, vk_code = get_key_info(part)
+ modifiers |= modifier_bits.get(part, 0)
+ params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': part, 'code': code, 'modifiers': modifiers}
if vk_code is not None:
params['windowsVirtualKeyCode'] = vk_code
+ if len(part) == 1 and not modifiers & 7:
+ params['text'] = part
+ held.append(part)
await self._client.send.Input.dispatchKeyEvent(params, session_id=session_id)
-
- # Press main key with modifiers bitmask
- main_code, main_vk_code = get_key_info(main_key)
- main_down_params: 'DispatchKeyEventParameters' = {
- 'type': 'keyDown',
- 'key': main_key,
- 'code': main_code,
- 'modifiers': modifier_value,
- }
- if main_vk_code is not None:
- main_down_params['windowsVirtualKeyCode'] = main_vk_code
- await self._client.send.Input.dispatchKeyEvent(main_down_params, session_id=session_id)
-
- main_up_params: 'DispatchKeyEventParameters' = {
- 'type': 'keyUp',
- 'key': main_key,
- 'code': main_code,
- 'modifiers': modifier_value,
- }
- if main_vk_code is not None:
- main_up_params['windowsVirtualKeyCode'] = main_vk_code
- await self._client.send.Input.dispatchKeyEvent(main_up_params, session_id=session_id)
-
- # Release modifier keys
- for mod in reversed(modifiers):
- code, vk_code = get_key_info(mod)
- release_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': mod, 'code': code}
+ await asyncio.sleep(duration)
+ finally:
+ for part in reversed(held):
+ modifiers &= ~modifier_bits.get(part, 0)
+ code, vk_code = get_key_info(part)
+ params = {'type': 'keyUp', 'key': part, 'code': code, 'modifiers': modifiers}
if vk_code is not None:
- release_params['windowsVirtualKeyCode'] = vk_code
- await self._client.send.Input.dispatchKeyEvent(release_params, session_id=session_id)
- else:
- # Simple key press
- code, vk_code = get_key_info(key)
- key_down_params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': key, 'code': code}
- if vk_code is not None:
- key_down_params['windowsVirtualKeyCode'] = vk_code
- await self._client.send.Input.dispatchKeyEvent(key_down_params, session_id=session_id)
-
- key_up_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': key, 'code': code}
- if vk_code is not None:
- key_up_params['windowsVirtualKeyCode'] = vk_code
- await self._client.send.Input.dispatchKeyEvent(key_up_params, session_id=session_id)
+ params['windowsVirtualKeyCode'] = vk_code
+ await self._client.send.Input.dispatchKeyEvent(params, session_id=session_id)
async def set_viewport_size(self, width: int, height: int) -> None:
"""Set the viewport size."""
diff --git a/browser_use/browser/events.py b/browser_use/browser/events.py
index 90aea0e9b..7084df0c1 100644
--- a/browser_use/browser/events.py
+++ b/browser_use/browser/events.py
@@ -241,6 +241,7 @@ class SendKeysEvent(BaseEvent[None]):
"""Send keyboard keys/shortcuts."""
keys: str # e.g., "ctrl+a", "cmd+c", "Enter"
+ target_id: TargetID | None = None # Actor pages can address a specific target.
event_timeout: float | None = Field(default_factory=lambda: _get_timeout('TIMEOUT_SendKeysEvent', 60.0)) # seconds
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.py
index 5c9c92e63..3d6b7f5ca 100644
--- a/browser_use/browser/watchdogs/default_action_watchdog.py
+++ b/browser_use/browser/watchdogs/default_action_watchdog.py
@@ -3,6 +3,7 @@
import asyncio
import json
import os
+from typing import Literal
from cdp_use.cdp.input.commands import DispatchKeyEventParameters
@@ -399,7 +400,7 @@ class DefaultActionWatchdog(BaseWatchdog):
if event.force:
self.logger.debug(f'Force clicking at coordinates ({event.coordinate_x}, {event.coordinate_y})')
return await self._execute_click_with_download_detection(
- self._click_on_coordinate(event.coordinate_x, event.coordinate_y, force=True)
+ self._click_on_coordinate(event.coordinate_x, event.coordinate_y, force=True, button=event.button)
)
# Get element at coordinates for safety checks
@@ -410,7 +411,7 @@ class DefaultActionWatchdog(BaseWatchdog):
f'No element found at coordinates ({event.coordinate_x}, {event.coordinate_y}), proceeding with click anyway'
)
return await self._execute_click_with_download_detection(
- self._click_on_coordinate(event.coordinate_x, event.coordinate_y, force=False)
+ self._click_on_coordinate(event.coordinate_x, event.coordinate_y, force=False, button=event.button)
)
# Safety check: file input
@@ -442,7 +443,7 @@ class DefaultActionWatchdog(BaseWatchdog):
# All safety checks passed, click at coordinates (with download detection)
return await self._execute_click_with_download_detection(
- self._click_on_coordinate(event.coordinate_x, event.coordinate_y, force=False)
+ self._click_on_coordinate(event.coordinate_x, event.coordinate_y, force=False, button=event.button)
)
except Exception:
@@ -1061,7 +1062,9 @@ class DefaultActionWatchdog(BaseWatchdog):
long_term_memory=error_detail,
)
- async def _click_on_coordinate(self, coordinate_x: int, coordinate_y: int, force: bool = False) -> dict | None:
+ async def _click_on_coordinate(
+ self, coordinate_x: int, coordinate_y: int, force: bool = False, button: Literal['left', 'right', 'middle'] = 'left'
+ ) -> dict | None:
"""
Click directly at coordinates using CDP Input.dispatchMouseEvent.
@@ -1100,7 +1103,7 @@ class DefaultActionWatchdog(BaseWatchdog):
'type': 'mousePressed',
'x': coordinate_x,
'y': coordinate_y,
- 'button': 'left',
+ 'button': button,
'clickCount': 1,
},
session_id=session_id,
@@ -1119,7 +1122,7 @@ class DefaultActionWatchdog(BaseWatchdog):
'type': 'mouseReleased',
'x': coordinate_x,
'y': coordinate_y,
- 'button': 'left',
+ 'button': button,
'clickCount': 1,
},
session_id=session_id,
@@ -2474,7 +2477,10 @@ class DefaultActionWatchdog(BaseWatchdog):
async def on_SendKeysEvent(self, event: SendKeysEvent) -> None:
"""Handle send keys request with CDP."""
- cdp_session = await self.browser_session.get_or_create_cdp_session(focus=True)
+ if event.target_id is not None:
+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=event.target_id, focus=True)
+ else:
+ cdp_session = await self.browser_session.get_or_create_cdp_session(focus=True)
try:
# Normalize key names from common aliases
key_aliases = {