getElement method
Returns List of elements found at specified address. Example address: "div.item > a.title" where item and title are class names of div and a tag respectively.
Sometimes the last address is not present consistently throughout the webpage. Use "extraAddress" to catch its attributes. Example extraAddress: "a"
Implementation
List<Map<String, dynamic>> getElement(String address, List<String> attribs,
{String? extraAddress}) {
// Attribs are the list of attributes required to extract from the html tag(s) ex. ['href', 'title'].
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);
// ignore: omit_local_variable_types
List<Map<String, dynamic>> elementData = [];
for (var element in elements) {
var attribData = <String, dynamic>{};
for (var attrib in attribs) {
if (extraAddress != null) {
var extraElement = element.querySelector(extraAddress);
if (extraElement != null) {
attribData[attrib] = extraElement.attributes[attrib];
}
} else {
attribData[attrib] = element.attributes[attrib];
}
}
elementData.add({
'title': element.text,
'attributes': attribData,
});
}
return elementData;
}