How to List Targets in a Puppeteer BrowserContext
Use `context.targets()` to get every active target in a Puppeteer BrowserContext. See how it differs from `pages()` and `browser.targets()`, with runnable examples and fixes for common issues.
Call context.targets() on the Puppeteer BrowserContext you want to inspect. It returns a Target[] synchronously, containing the active targets in that context. This is the context-scoped API; browser.targets() covers the whole browser, while context.pages() returns page objects and omits non-visible pages by default.
const targets = context.targets();
console.log(targets);
Minimal runnable example
Install Puppeteer, save this as list-targets.js, and run it with Node.js. The script creates an isolated context, opens a page, lists its targets, then closes the browser.
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
try {
const context = await browser.createBrowserContext();
const page = await context.newPage();
await page.goto('https://example.com');
const targets = context.targets();
console.log(`Active targets in this context: ${targets.length}`);
for (const target of targets) {
console.log({ type: target.type(), url: target.url() });
}
} finally {
await browser.close();
}
The example uses top-level await, supported in Node.js ES modules. If your project uses CommonJS, put the body inside an async function main() and call main().catch(console.error). The returned entries are Puppeteer Target objects; use their methods to inspect or interact with the target as needed.
Targets, pages, and browser-wide scope
| Need | API | Result and scope |
|---|---|---|
| All active targets in one context | context.targets() |
Synchronous Target[], limited to that context. |
| Pages in one context | await context.pages() |
Asynchronous Page[]; non-visible pages are omitted by default. |
| All active targets in the browser | browser.targets() |
Targets across all browser contexts. |
| Open contexts | browser.browserContexts() |
The browser’s BrowserContext[]. |
Choose based on both scope and what you want to do. A target is the broader inspection unit; a page is the page-oriented object. If the requirement says “all targets in this context,” use context.targets(). If you need page objects to use page APIs, use context.pages(), keeping its default visibility limitation in mind. The BrowserContext.targets() API reference, BrowserContext.pages() reference, and Browser.targets() reference document these distinctions.
Inspect targets in one context
Keep the context reference returned by the browser and call targets() on that object. This prevents accidentally including targets from another context.
const context = await browser.createBrowserContext();
const page = await context.newPage();
await page.goto('https://example.com');
const targets = context.targets();
for (const target of targets) {
console.log(target.type(), target.url());
}
The method itself is synchronous, so do not add await to it. Operations that create targets, such as opening a page or a popup, are asynchronous; wait for those operations before taking the snapshot if you need to include their result.
Include non-visible pages
context.pages() is convenient when you need the context’s page objects, but by default it does not include non-visible pages such as background pages. The Puppeteer documentation notes that these can be found using Target.page(). The includeAll option for pages() is documented as experimental, so use it only with that status in mind.
const targets = context.targets();
for (const target of targets) {
const page = await target.page();
if (page) {
console.log('Page target:', target.url());
} else {
console.log('Target has no page object:', target.type(), target.url());
}
}
Target.page() may resolve to no page for target types that are not pages. Handle that case instead of assuming every target can be converted into a Page.
Understand context ownership
A BrowserContext represents a user context with isolated storage. A popup opened by a page belongs to the browser context of its opener. Keep and inspect the opener’s context when you want to include that popup. See the BrowserContext API reference for context behavior and lifecycle.
const context = await browser.createBrowserContext();
const opener = await context.newPage();
await opener.goto('https://example.com');
// After the page opens a popup, inspect this same context.
const targets = context.targets();
console.log(targets.map(target => ({ type: target.type(), url: target.url() })));
For deterministic automation, wait for the event or action that opens the popup before calling targets(). The returned array reflects active targets at the time of the call; it is not a subscription that updates when later targets appear.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
context.targets is not a function |
The variable is not a Puppeteer BrowserContext, or the wrong object was retained. |
Check that it came from browser.createBrowserContext() or browser.defaultBrowserContext(), and inspect the variable before calling the method. |
| The array is empty or misses a newly opened page | The page or popup has not been created yet when the snapshot is taken. | Await the page creation or popup event/action first, then call context.targets() again. |
A background page is missing from pages() |
pages() omits non-visible pages by default. |
Use context.targets() and, where appropriate, call target.page(); handle targets that have no page. |
| Targets from another context appear | The code called browser.targets(), which has browser-wide scope. |
Call targets() on the specific context instead. |
| The browser exits before results are used | The context or browser was closed too early, often by cleanup running before inspection completes. | Finish listing and processing targets before closing the browser. Close the browser in a finally block. |
Performance and reliability notes
context.targets()is synchronous and gives you a point-in-time array. Call it after the actions whose results you need to inspect.- It avoids scanning targets from unrelated contexts when context scope matters. Use
browser.targets()only when browser-wide coverage is intended. - Do not assume every target is a visible page or has a
Pageobject. Check target type and handle a missing page fromtarget.page(). - Close the browser when the job is complete. The BrowserContext documentation notes that closing the browser closes its associated pages.
The method only lists targets in the running Puppeteer browser; it does not capture screenshots or create a hosted screenshot service request. Its direct cost is the work and browser resources in your own automation environment.
Or skip the browser setup
If your actual goal is a website screenshot rather than inspecting Puppeteer targets, ScreenshotNeo provides a website screenshot API and MCP server. Its [documentation](https://screenshotneo.com/docs/) has the request options. This one-call example saves the response as a WebP image:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
ScreenshotNeo accepts cookie banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before the shot; each cleanup step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and billing status. Its MCP server gives AI agents tools to take screenshots, get page information, and capture PDFs. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 screenshots.
Sign up for 1,000 free screenshots a month, with no card.
FAQ
Is context.targets() asynchronous?
No. It returns Target[] synchronously. The pages() method is the one that returns a promise.
Does browser.targets() include every context?
Yes. It is browser-wide. Use the specific context’s targets() method when you need context-only results.
Do popups belong to a separate context?
A popup belongs to the browser context of the page that opened it, so inspect that opener’s context.


