Back to Puppeteer

BrowserContext.waitForTarget() method

docs/api/puppeteer.browsercontext.waitfortarget.md

19.2.21.1 KB
Original Source

BrowserContext.waitForTarget() method

Waits until a target matching the given predicate appears and returns it.

This will look all open browser contexts.

Signature

typescript
class BrowserContext {
  waitForTarget(
    predicate: (x: Target) => boolean | Promise<boolean>,
    options?: WaitForTargetOptions,
  ): Promise<Target>;
}

Parameters

<table><thead><tr><th>

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

predicate

</td><td>

(x: Target) => boolean | Promise<boolean>

</td><td> </td></tr> <tr><td>

options

</td><td>

WaitForTargetOptions

</td><td>

(Optional)

</td></tr> </tbody></table>

Returns:

Promise<Target>

Example

Finding a target for a page opened via window.open:

ts
await page.evaluate(() => window.open('https://www.example.com/'));
const newWindowTarget = await browserContext.waitForTarget(
  target => target.url() === 'https://www.example.com/',
);