getElementTitle method

List<String> getElementTitle(
  1. String address
)

Returns List of elements titles found at specified address. Example address: "div.item > a.title" where item and title are class names of div and a tag respectively. For ease of access, when using Chrome inspection tool, right click the item you want to copy, then click "Inspect" and at the console, right click the highlighted item, right click and then click "Copy > Copy selector" and provide as String address parameter to this method.

Implementation

List<String> getElementTitle(String address) {
  if (_document == null) {
    throw WebScraperException(
        'getElement cannot be called before loadWebPage');
  }
  // Using query selector to get a list of particular element.
  var elements = _document!.querySelectorAll(address);

  var elementData = <String>[];

  for (var element in elements) {
    // Checks if the element's text is null before adding it to the list.
    if (element.text.trim() != '') {
      elementData.add(element.text);
    }
  }
  return elementData;
}