waitForSelector method

Future<ElementHandle?> waitForSelector(
  1. String selector, {
  2. bool? visible,
  3. bool? hidden,
  4. Duration? timeout,
})

Wait for the selector to appear in page. If at the moment of calling the method the selector already exists, the method will return immediately. If the selector doesn't appear after the timeout of waiting, the function will throw.

This method works across navigations:

import 'package:puppeteer/puppeteer.dart';

void main() async {
  var browser = await puppeteer.launch();
  var page = await browser.newPage();
  var watchImg = page.waitForSelector('img');
  await page.goto('https://example.com');
  var image = await watchImg;
  print(await image!.propertyValue('src'));
  await browser.close();
}

Shortcut for page.mainFrame.waitForSelector.

Parameters:

  • A selector of an element to wait for
  • visible: wait for element to be present in DOM and to be visible, i.e. to not have display: none or visibility: hidden CSS properties. Defaults to false.
  • hidden: wait for element to not be found in the DOM or to be hidden, i.e. have display: none or visibility: hidden CSS properties. Defaults to false.
  • timeout: maximum time to wait for. Pass Duration.zero to disable timeout. The default value can be changed by using the page.defaultTimeout property.

Returns a Future which resolves when element specified by selector string is added to DOM. Resolves to null if waiting for hidden: true and selector is not found in DOM.

Implementation

Future<ElementHandle?> waitForSelector(String selector,
    {bool? visible, bool? hidden, Duration? timeout}) {
  return mainFrame.waitForSelector(selector,
      visible: visible, hidden: hidden, timeout: timeout);
}