helpers: CDP attach lost after goto_url in the same run — capture_screenshot fails with 'Not attached to an active page'
Version: browser-harness 0.1.13 (pip), Python 3.12, Linux container, local Chrome driven over CDP.
Usage pattern: one browser-harness process per step: Python code on stdin, results on stdout, Chrome already running and shared via CDP.
Repro
Two statements in the same run — navigate, then capture:
echo 'goto_url("https://example.com"); print(capture_screenshot("/tmp/p.png"))' | browser-harness
RuntimeError: {'code': -32000, 'message': 'Not attached to an active page'}
Same failure with a file:// target. Splitting the same two statements into two separate runs works:
echo 'goto_url("https://example.com")' | browser-harness # ok
echo 'print(capture_screenshot("/tmp/p.png"))' | browser-harness # ok, PNG written
What it looks like
Navigation replaces the page target, and the CDP session the daemon attached to at run start is not re-established for the remainder of the run — every subsequent CDP call in that same run fails with -32000 Not attached to an active page. A fresh run attaches anew, which is why splitting into two runs works. (Hypothesis, from reading daemon.py: attach_first_page establishes the session at startup, and a mid-run navigation can invalidate it without a re-attach path.)
Why this matters
"Navigate, then inspect/screenshot what loaded" is the most natural pattern for an LLM-driven agent executing code snippets against a page. Today it silently forces a two-run dance, and the error message doesn't point at navigation as the cause.
Ask
Re-attach to the current page after a navigation within the same run (e.g. detect the lost target and call the same session-replacement path), or expose an explicit reattach() helper that callers can use after goto_url.
Hi, I'd like to work on this issue.
I'll first reproduce the CDP session loss on current main using the provided goto_url() + capture_screenshot() case, then trace the session lifecycle around navigation and propose the smallest fix with regression coverage.
I'll post my findings here before opening the PR.
I reproduced this on current main on Windows 10 / Python 3.12.15 with local Chrome over CDP.
A few findings suggest this is not actually a lost target/session:
- The target ID remains identical before and after
goto_url(). page_info()/Runtime.evaluateworks immediately after the navigation in the same run.capture_screenshot()immediately aftergoto_url()can fail with-32000 Not attached to an active page(and in some runs can remain blocked until its timeout).- A clean run with
goto_url(...), followed bywait_for_load(), followed bycapture_screenshot(...)succeeds. - I also verified clean runs with a short post-navigation delay; 500 ms and 1 s both succeeded in my environment.
This also seems consistent with the existing Browser Harness usage contract: SKILL.md says to call wait_for_load() after navigation, and the repository's domain-skill examples generally use goto_url() followed by wait_for_load().
So the original hypothesis that navigation replaces the target and requires a CDP re-attach does not appear to hold in my reproduction. Re-attaching on the Not attached to an active page error may also be risky because it could select a different open tab unless recovery is explicitly bound to the tracked target.
Would you prefer this issue to be treated as:
- documented usage (
goto_url()is intentionally non-blocking and callers shouldwait_for_load()), - a documentation/error-message improvement, or
- a robustness improvement where screenshot handling waits/retries safely during the short navigation transition?
I have a regression test setup locally, but I have not changed production code yet because I wanted to confirm the intended API semantics first.
Thanks for the careful reproduction — the identical target ID and the working Runtime.evaluate right after navigation are convincing: this is the navigation-transition window, not a lost session.
My preference: 3 + 2, with 1 as the documented contract.
goto_url()staying non-blocking is fine, but the failure is misleading:-32000 "Not attached to an active page"points at CDP re-attach (exactly the wrong hypothesis I started from) while the actual remedy iswait_for_load()/ a short settle. A bounded wait-and-retry insidecapture_screenshot— bound to the tracked target, never re-selecting a tab — would remove the footgun for single-process pipelines.- Keep the retry short and bounded (e.g., wait for the tracked target's ready state, ~1–2 s ceiling) so genuine failures still surface quickly, and make the error message mention
wait_for_load()when the transition is the cause. - Agreed on avoiding auto re-attach, for the tab-selection reason you gave.
A regression test mirroring the single-run repro (goto_url(...); capture_screenshot(...)) would be ideal. No objection to you opening the PR along those lines — thanks for picking it up.