Skip to content

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

fix(#3889): Draft.js editors lose first character when typing

zhongzhenyuc0f4f395701e
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.py
index a698a3ef7..c60fc2858 100644
--- a/browser_use/browser/watchdogs/default_action_watchdog.py
+++ b/browser_use/browser/watchdogs/default_action_watchdog.py
@@ -1549,6 +1549,29 @@ class DefaultActionWatchdog(BaseWatchdog):
else:
self.logger.debug(f'🎯 Typing text character by character: "{text}"')
+ # FIX for Issue #3889: Draft.js editors lose the first character because
+ # CDP's dispatchKeyEvent doesn't trigger the beforeinput event that
+ # Draft.js relies on.
+ #
+ # Solution: Detect Draft.js editors specifically (not all contenteditable
+ # elements) and prepend a "sacrifice" space character that will be
+ # dropped by Draft.js, so the actual text starts from the second character.
+ #
+ # Draft.js detection: Check for Draft.js-specific class names and data attributes
+ _attrs = element_node.attributes or {}
+ _class_name = _attrs.get('class', '')
+ _is_draftjs = (
+ 'DraftEditor' in _class_name # Draft.js editor class (e.g., public-DraftEditor-content)
+ or _attrs.get('data-contents') == 'true' # Draft.js content container
+ or _attrs.get('data-block') == 'true' # Draft.js block element
+ or 'data-offset-key' in _attrs # Draft.js offset key for content tracking
+ )
+
+ if _is_draftjs and len(text) > 0:
+ # Prepend a space as "sacrifice character" - it will be dropped by Draft.js
+ text = ' ' + text
+ self.logger.debug('🎯 Draft.js detected, prepending sacrifice space')
+
for i, char in enumerate(text):
# Handle newline characters as Enter key
if char == '\n':