Skip to content

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

always activate target before input_text

Nick Sweeting2b4afd8a19b0
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.py
index bb720d38b..22a6bd20d 100644
--- a/browser_use/browser/default_action_watchdog.py
+++ b/browser_use/browser/default_action_watchdog.py
@@ -497,34 +497,34 @@ class DefaultActionWatchdog(BaseWatchdog):
"""
try:
# Get CDP client and session
- cdp_client = self.browser_session.cdp_client
- session_id = (await self.browser_session.get_or_create_cdp_session()).session_id
+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=None, focus=True)
+ await cdp_session.cdp_client.send.Target.activateTarget(params={'targetId': cdp_session.target_id})
# Type the text character by character to the focused element
for char in text:
# Send keydown
- await cdp_client.send.Input.dispatchKeyEvent(
+ await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
params={
'type': 'keyDown',
'key': char,
},
- session_id=session_id,
+ session_id=cdp_session.session_id,
)
# Send char for actual text input
- await cdp_client.send.Input.dispatchKeyEvent(
+ await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
params={
'type': 'char',
'text': char,
},
- session_id=session_id,
+ session_id=cdp_session.session_id,
)
# Send keyup
- await cdp_client.send.Input.dispatchKeyEvent(
+ await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
params={
'type': 'keyUp',
'key': char,
},
- session_id=session_id,
+ session_id=cdp_session.session_id,
)
# Add 18ms delay between keystrokes
await asyncio.sleep(0.018)