diff --git a/packages/puppeteer-core/src/cdp/FrameTree.ts b/packages/puppeteer-core/src/cdp/FrameTree.ts index 7ee1b86b5fa..dcd341c9e33 100644 --- a/packages/puppeteer-core/src/cdp/FrameTree.ts +++ b/packages/puppeteer-core/src/cdp/FrameTree.ts @@ -21,6 +21,7 @@ export class FrameTree { // frameID -> childFrameIDs #childIds = new Map>(); #mainFrame?: FrameType; + #isMainFrameStale = false; #waitRequests = new Map>>(); getMainFrame(): FrameType | undefined { @@ -59,8 +60,9 @@ export class FrameTree { this.#childIds.set(frame._parentId, new Set()); } this.#childIds.get(frame._parentId)!.add(frame._id); - } else if (!this.#mainFrame) { + } else if (!this.#mainFrame || this.#isMainFrameStale) { this.#mainFrame = frame; + this.#isMainFrameStale = false; } this.#waitRequests.get(frame._id)?.forEach(request => { return request.resolve(frame); @@ -73,7 +75,7 @@ export class FrameTree { if (frame._parentId) { this.#childIds.get(frame._parentId)?.delete(frame._id); } else { - this.#mainFrame = undefined; + this.#isMainFrameStale = true; } }