extract_text method

Future<Online> extract_text(
  1. AbstractSelector selector,
  2. String key, {
  3. String? attribute,
})

Extracts text content from elements matching the selector and stores it in the context.

selector is the AbstractSelector to find elements. key is the key under which the extracted data will be stored in the context. attribute (optional) extracts the specified attribute instead of text content.

Returns the Online instance for method chaining.

Example usage:

await online
  .extract_text(Css('h1'), 'headings')
  .extract_text(Css('a'), 'linkUrls', attribute: 'href');
var headings = ctx.get<List<String>>('headings');
var linkUrls = ctx.get<List<String>>('linkUrls');

Implementation

Future<Online> extract_text(AbstractSelector selector, String key,
    {String? attribute}) async {
  Show.action('extracting', attribute ?? 'text', 'from', selector.selector);
  await (await page).waitForSelector(selector.selector);
  var extracted = await (await page).evaluate('''(selector, attribute) => {
  return Array.from(document.querySelectorAll(selector)).map(e =>
    attribute ? e.getAttribute(attribute) : e.textContent.trim()
  );
}''', args: [selector.selector, attribute]);
  ctx.set(key, List<String>.from(extracted));
  return this;
}