waitForElement method

Future<bool> waitForElement(
  1. String selector, {
  2. int timeoutMillis = 10000,
})

Waits for an element to be present in the DOM

Implementation

Future<bool> waitForElement(
  String selector, {
  int timeoutMillis = 10000,
}) async {
  _checkDisposed();

  if (_controller == null) {
    throw ScrapingException.validation(
      'Browser not initialized',
      isRetryable: false,
    );
  }

  final startTime = DateTime.now();
  while (DateTime.now().difference(startTime).inMilliseconds <
      timeoutMillis) {
    final result = await _controller!.evaluateJavascript(
      source: "document.querySelector('$selector') != null",
    );

    if (result == true) {
      return true;
    }

    await Future.delayed(const Duration(milliseconds: 100));
  }

  return false;
}