--- id: "c739c130-a204-4a3b-a2c0-40cd45bdaf09" name: "Switch Tab Session Binding" micro_summary: "Captures the session_id from switch_tab() for CDP mouse events and reuses it, avoiding events being sent to another tab." status: "resolved" see_also: [] scope: "How switch_tab() returns a CDP session_id that must be used for Input.dispatchMouseEvent. Without the explicit session_id, mouse events go to the wrong tab." warnings: "Always capture and reuse the session_id from switch_tab(). Don't call switch_tab multiple times or attach separately — each creates a new session." --- ## How It Works `switch_tab(targetId)` returns a CDP session_id. This session must be passed explicitly to every `cdp("Input.dispatchMouseEvent", ...)` call: ```python sid = switch_tab("340ABBFAEC9A82B9FD65ECD77020B23C") # later... cdp("Input.dispatchMouseEvent", type="mousePressed", x=380, y=400, button="left", clickCount=1, session_id=sid) ``` ## Why This Matters After `switch_tab()`, `js()` runs correctly on the Gmail tab (it uses the default session). But `cdp("Input.dispatchMouseEvent", ...)` and `click_at_xy()` send events to whichever CDP session is active — which may NOT be Gmail's if a subsequent `js(target_id=...)` call attached to a different target. ## Key Findings - Each `switch_tab()` call creates a NEW session. Reuse the returned `sid`. - Don't call `Target.attachToTarget` again — that creates a separate session and splits the context. - `click_at_xy()` does NOT use the session from `switch_tab()`. It uses the default cdp() path. - Verified: events sent without session_id are received by a different tab (confirmed by mousedown listener not firing on Gmail).