fix: dispatch TabCreatedEvent for popups created by window.open()#4239
Open
OiPunk wants to merge 2 commits intobrowser-use:mainfrom
Open
fix: dispatch TabCreatedEvent for popups created by window.open()#4239OiPunk wants to merge 2 commits intobrowser-use:mainfrom
OiPunk wants to merge 2 commits intobrowser-use:mainfrom
Conversation
…pen) When a popup is created via window.open(), the browser attaches the new target through CDP's Target.attachedToTarget. SessionManager handled the attachment correctly (enabling page monitoring, creating CDP sessions) but never dispatched TabCreatedEvent for these externally-created targets. This meant all event-driven watchdogs — PopupsWatchdog, AboutBlankWatchdog, SecurityWatchdog, etc. — never initialized for the popup tab. The practical symptoms: dialog handlers missing, viewport settings not applied, and the popup stuck on about:blank while the original page appeared frozen. The fix dispatches TabCreatedEvent from _handle_target_attached() for page/ tab targets. All existing watchdog on_TabCreatedEvent handlers are already idempotent, so the potential duplicate dispatch (for internally-created tabs that also emit the event from the caller) is harmless. Closes browser-use#4208
The popup target's initial URL can be an empty string (not absent) when
Chrome attaches it. Using `target_info.get('url', 'about:blank')` only
covers the absent-key case; `get('url') or 'about:blank'` handles both
absent and empty-string, preventing SecurityWatchdog from closing the
popup as "disallowed URL".
Also simplified the test suite:
- Replaced fragile tab-count assertion with event-based URL validation
- Removed internal-attribute test (PopupsWatchdog._dialog_listeners_registered)
- Kept core event dispatch test + URL validity test + original page test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4208 —
window.open()causes the previous page to freeze and new page stays onabout:blank.Root cause
When a popup is created via
window.open(), Chrome attaches the new target through CDPTarget.attachedToTarget.SessionManager._handle_target_attached()correctly enables page monitoring and creates a CDP session, but never dispatchedTabCreatedEventfor externally-created targets.Without
TabCreatedEvent, none of the event-driven watchdogs initialize for the popup:The result: popup stuck on
about:blank, original page appears frozen.Fix
Dispatch
TabCreatedEventfrom_handle_target_attached()forpage/tabtargets, right after_enable_page_monitoring(). This is the single place where all externally-created targets (popups, new tabs from links,window.open()) converge.All existing
on_TabCreatedEventhandlers are idempotent:PopupsWatchdog— guards with_dialog_listeners_registeredsetAboutBlankWatchdog— guards with__dvdAnimationRunningJS flagCrashWatchdog— guards with_targets_with_listenerssetSecurityWatchdog/DownloadsWatchdog/DOMWatchdog— stateless checksSo the potential duplicate dispatch for internally-created tabs (which also emit
TabCreatedEventfrom the caller) is safe.Changes
browser_use/browser/session_manager.py— 12-line addition in_handle_target_attached()tests/ci/browser/test_popup_tab_created_event.py— 4 regression tests covering the event dispatch, URL navigation, watchdog initialization, and original page stabilityTest plan
ruff check/ruff format --checkpasspytest tests/ci/browser/test_popup_tab_created_event.py -v -sSummary by cubic
Fixes #4208 by dispatching TabCreatedEvent for window.open() popups so they initialize and don’t freeze on about:blank. Also defaults empty popup URLs to about:blank to satisfy SecurityWatchdog, keeping the opener page responsive.
Written for commit ae0a7a2. Summary will update on new commits.