Puppeteer: Requesting main frame too early!
The frame tree has no main frame when mainFrame is requested.
The frame tree has no main frame when mainFrame is requested.
Error message
Requesting main frame too early!
How this error happens
Puppeteer’s frame tree has no main frame yet. A partially initialized page from an integration, or a frame tree being torn down, cannot satisfy mainFrame.
The failing snippet uses an existing page; the correction uses a live browser.
await page.close();
const frame = page.mainFrame();
await frame.evaluate(() => {
return document.title;
});
How to fix
Obtain the page through an awaited public creation call and keep it open while using its frame.
const page = await browser.newPage();
const frame = page.mainFrame();
const title = await frame.evaluate(() => {
return document.title;
});
console.log(title);
Things to keep in mind
The failing sequence demonstrates stale page/frame use. It may be caught by a detached-frame error instead of the internal no-main-frame check. If an awaited newPage still lacks a main frame, isolate the browser/integration failure rather than adding an arbitrary sleep.