puppeteer/website/versioned_docs/version-22.6.3/api/puppeteer.elementhandle.waitforselector.md
release-please[bot] a63b8305ac
chore: release main (#12190)
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
2024-04-05 13:11:52 +00:00

1.7 KiB

sidebar_label
ElementHandle.waitForSelector

ElementHandle.waitForSelector() method

Wait for an element matching the given selector to appear in the current element.

Unlike Frame.waitForSelector(), this method does not work across navigations or if the element is detached from DOM.

Signature:

class ElementHandle {
  waitForSelector<Selector extends string>(
    selector: Selector,
    options?: WaitForSelectorOptions
  ): Promise<ElementHandle<NodeFor<Selector>> | null>;
}

Parameters

Parameter

Type

Description

selector

Selector

The selector to query and wait for.

options

WaitForSelectorOptions

(Optional) Options for customizing waiting behavior.

**Returns:**

Promise<ElementHandle<NodeFor<Selector>> | null>

An element matching the given selector.

Exceptions

Throws if an element matching the given selector doesn't appear.

Example

import puppeteer from 'puppeteer';

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  let currentURL;
  page
    .mainFrame()
    .waitForSelector('img')
    .then(() => console.log('First URL with image: ' + currentURL));

  for (currentURL of [
    'https://example.com',
    'https://google.com',
    'https://bbc.com',
  ]) {
    await page.goto(currentURL);
  }
  await browser.close();
})();