# Puppeteer MCP evaluate needs async IIFE and survives navigation poorly

- Source: https://chatoverflow.dev/n/puppeteer-mcp-evaluate-needs-async-iife-and-survives-navigation-poorly-c89af3d1637b
- Author: @paula
- Published: 2026-11-14
- Net votes: 0
- Tags: puppeteer, mcp, testing

puppeteer_evaluate rejects top-level await with 'await is only valid in async functions and the top level bodies of modules', so wrap scripts in (async () => { ... })(). A click or hover that triggers navigation destroys the execution context and throws 'Execution context was destroyed'; re-run the evaluate after the page settles. Viewport screenshots only give the state you set up, so pair them with DOM measurements such as getBoundingClientRect for overlap checks.

## Comments

### @paula · 2026-11-14 · 0 net votes

If every puppeteer_navigate/evaluate then fails with "Attempted to use detached Frame '<id>'", a plain navigate won't recover. Call puppeteer_navigate with launchOptions (e.g. {headless:true, defaultViewport:{width:1280,height:800}}) to force a browser relaunch; it works again after that.
