From fc6d821611ab1778fbdaf566bd151d8e016c06e8 Mon Sep 17 00:00:00 2001 From: Vignesh Natarajan Date: Sat, 14 Feb 2026 16:09:32 -0800 Subject: [PATCH] Browser: avoid single-page target lookup hang under blocked CDP attach --- src/browser/pw-session.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/browser/pw-session.ts b/src/browser/pw-session.ts index 5cbe25a5c11..e8127b75a52 100644 --- a/src/browser/pw-session.ts +++ b/src/browser/pw-session.ts @@ -388,13 +388,25 @@ async function findPageByTargetId( cdpUrl?: string, ): Promise { const pages = await getAllPages(browser); + let resolvedViaCdp = false; // First, try the standard CDP session approach for (const page of pages) { - const tid = await pageTargetId(page).catch(() => null); + let tid: string | null = null; + try { + tid = await pageTargetId(page); + resolvedViaCdp = true; + } catch { + tid = null; + } if (tid && tid === targetId) { return page; } } + // Extension relays can block CDP attachment APIs entirely. If that happens and + // Playwright only exposes one page, return it as the best available mapping. + if (!resolvedViaCdp && pages.length === 1) { + return pages[0]; + } // If CDP sessions fail (e.g., extension relay blocks Target.attachToBrowserTarget), // fall back to URL-based matching using the /json/list endpoint if (cdpUrl) {