ScreenshotNeo

BlogHow-to

Get a Puppeteer Browser’s Target

Use `browser.target()` to get the target for Puppeteer’s default browser context. Learn when to use `browser.targets()` or `context.targets()` instead.

By the ScreenshotNeo team4 October 20265 min read

Call browser.target() to get the target associated with Puppeteer’s default browser context:

const target = browser.target();

It returns a Puppeteer Target object. Use browser.targets() to list active targets across browser contexts, or context.targets() to list targets in a specific context. A target is a Chrome DevTools Protocol (CDP) concept and can represent something other than a page.

Get the default browser target

Here is a complete Node.js example using Puppeteer. It launches a browser, gets its default-context target, prints its type and URL, and closes the browser:

const puppeteer = require('puppeteer');

async function main() {
  const browser = await puppeteer.launch();

  try {
    const target = browser.target();
    console.log('Target type:', target.type());
    console.log('Target URL:', target.url());
  } finally {
    await browser.close();
  }
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

browser.target() is synchronous: it returns the target object directly, so you do not need to await it. The example uses await for launch and shutdown because those browser operations are asynchronous.

See the Puppeteer Browser.target() API reference. API details can vary by release; check the documentation matching the Puppeteer version installed in your project.

Choose the target method by scope

What you need Method What it returns
The target associated with the default browser context browser.target() One Target
All active targets across browser contexts browser.targets() An array of active Target objects
Active targets in one browser context context.targets() An array of targets belonging to that context

These methods have different scopes. Use the browser-level list when you need to discover targets across contexts; use the context-level list when you already know which context to inspect.

List targets across the browser

const targets = browser.targets();

for (const target of targets) {
  console.log(target.type(), target.url());
}

See the Browser.targets() API reference.

List targets in one context

const context = await browser.createBrowserContext();

try {
  const page = await context.newPage();
  await page.goto('https://example.com');

  for (const target of context.targets()) {
    console.log(target.type(), target.url());
  }
} finally {
  await context.close();
}

A newly created context provides an isolated scope for its pages and other targets. context.targets() keeps enumeration within that context.

Work with a Target safely

Do not assume a target always has a page. Targets can represent a page, frame, worker, or another CDP target. Calling target.page() returns a Page when the target is page-compatible, such as a page, webview, or background page; otherwise it returns null.

const target = browser.target();
const page = await target.page();

if (page) {
  console.log('Page URL:', page.url());
} else {
  console.log('This target has no page-compatible object.');
}

For a target’s owning browser, use target.browser(). If you already have a Page and need its browser, use page.browser().

Do not use the obsolete Page.target() method

The current Puppeteer reference marks page.target() as obsolete. Choose the replacement based on what you are trying to do:

  • Need a CDP session for a page: call page.createCDPSession().
  • Need to discover a popup opened by a page: use the page’s popup event.
  • Need the Browser from a Page: call page.browser().
// Attach a CDP session to a page.
const session = await page.createCDPSession();

// Get the Browser that owns a page.
const browser = page.browser();

Consult the Page.target() API reference for the current status and documented alternatives.

Common errors and fixes

Problem Likely cause Fix
browser.target is not a function The value named browser is not the Puppeteer Browser instance you expect, or the installed version’s API differs. Check where the value comes from and inspect the documentation for your installed Puppeteer version. Confirm that you called puppeteer.launch() or connected with Puppeteer and retained the returned browser.
target.page() returns null The target is not page-compatible; targets can represent workers or other CDP objects. Check the target’s type() before using page methods, and handle the null result.
The target list is missing targets you expected You enumerated a narrower scope, or the target had not been created when you read the list. Use browser.targets() for active targets across contexts, or context.targets() for one context. If creation is asynchronous, wait for the relevant page, popup, or target event before inspecting.
A warning says page.target() is obsolete Code uses the deprecated method. Use page.createCDPSession() for CDP work, the popup event for popup discovery, or page.browser() to get the owning browser.
Cannot read properties of undefined while calling the method The browser variable was not initialized, or browser launch/connect failed earlier. Make sure launch or connect completed successfully before calling browser.target(); inspect and handle the earlier error.

Performance and reliability notes

  • browser.target() reads the default-context target directly; use it when that is the target you need instead of enumerating targets and filtering.
  • browser.targets() and context.targets() return active targets. Treat these as a snapshot of what is active when you call them; wait for target creation when your flow depends on a new page or popup.
  • Use try/finally to close browsers and contexts even when page work fails. This avoids leaving browser processes running after an exception.
  • Target retrieval itself does not capture a screenshot. For screenshots, browser setup, and image output, use Puppeteer’s page capture APIs or a screenshot service.

Or skip the browser setup

If your goal is a website screenshot rather than CDP target inspection, ScreenshotNeo provides a one-request screenshot API. See the ScreenshotNeo API documentation for options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
const fs = require('node:fs/promises');
await fs.writeFile('shot.webp', Buffer.from(await res.arrayBuffer()));

ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture. Bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots.

Sign up free for 1,000 screenshots a month, with no card required.

FAQ

Does browser.target() return a Page?

No. It returns a Target. Use target.page() if you need a page, and handle the possibility that it returns null.

How do I get all targets?

Call browser.targets() for active targets across browser contexts, or context.targets() to limit the list to one context.

Is browser.target() asynchronous?

No. It returns the target directly, so there is no need to await the call.