navigateTo method

Future<bool> navigateTo(
  1. String url, {
  2. Map<String, String>? headers,
  3. int? timeoutMillis,
})

Navigates to the specified URL

Implementation

Future<bool> navigateTo(
  String url, {
  Map<String, String>? headers,
  int? timeoutMillis,
}) async {
  _checkDisposed();

  if (_controller == null) {
    await initialize();
  }

  if (_controller == null) {
    throw ScrapingException.validation(
      'Failed to initialize headless browser',
      isRetryable: false,
    );
  }

  // Reset page load completer
  _pageLoadCompleter = Completer<bool>();

  // Combine headers
  final combinedHeaders = <String, String>{};
  if (config.customHeaders != null) {
    combinedHeaders.addAll(config.customHeaders!);
  }
  if (headers != null) {
    combinedHeaders.addAll(headers);
  }

  // Load URL
  await _controller!.loadUrl(
    urlRequest: URLRequest(url: WebUri(url), headers: combinedHeaders),
  );

  // Wait for page to load with timeout
  try {
    return await _pageLoadCompleter!.future.timeout(
      Duration(milliseconds: timeoutMillis ?? config.timeoutMillis),
    );
  } on TimeoutException {
    _log('Navigation to $url timed out', isError: true);
    return false;
  } catch (e) {
    _log('Error navigating to $url: $e', isError: true);
    return false;
  }
}