vicentemovil commited on
Commit
df51c50
·
1 Parent(s): f8c0c02

Fix capture/record targeting: select real tab (type=page) not extension background page

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -369,16 +369,22 @@ def _launch_chromium(w: int, h: int):
369
 
370
 
371
  async def _connect_cdp(port: int, log):
372
- """Wait for the Chrome DevTools websocket URL and return it."""
 
 
 
 
 
373
  async with aiohttp.ClientSession() as session:
374
  for attempt in range(20):
375
  await asyncio.sleep(0.5)
376
  try:
377
  async with session.get(f"http://localhost:{port}/json/list") as resp:
378
- pages = await resp.json()
379
- if pages:
 
380
  log(f"chrome devtools ready after {attempt+1} attempts")
381
- return pages[0]["webSocketDebuggerUrl"]
382
  except Exception as e:
383
  log(f"devtools attempt {attempt+1} failed: {e}")
384
  continue
 
369
 
370
 
371
  async def _connect_cdp(port: int, log):
372
+ """Wait for the Chrome DevTools websocket URL and return it.
373
+
374
+ /json/list returns several targets (extension background pages, browser_ui,
375
+ etc.). We must pick the actual tab (type == "page") — pages[0] may be an
376
+ extension background_page, and navigating that would never reach the URL.
377
+ """
378
  async with aiohttp.ClientSession() as session:
379
  for attempt in range(20):
380
  await asyncio.sleep(0.5)
381
  try:
382
  async with session.get(f"http://localhost:{port}/json/list") as resp:
383
+ targets = await resp.json()
384
+ page = next((t for t in targets if t.get("type") == "page"), None)
385
+ if page:
386
  log(f"chrome devtools ready after {attempt+1} attempts")
387
+ return page["webSocketDebuggerUrl"]
388
  except Exception as e:
389
  log(f"devtools attempt {attempt+1} failed: {e}")
390
  continue