So, I'm trying to scrape web in order to get their elements link as well as id. However, it throws me an undefined as a result. I know that these classes exist because I use page.evaluate
, which gets specified class without hesitation. Cannot figure out why it is not working. Moreover, it looks like, that the console.log
is not being printed too
const flats = await page.$$eval(".result-item-v3", (elements) => {
console.log(elements.length);
return elements.forEach((element) => {
return {
link: element.querySelector(".object-image-link").getAttribute("href"),
id: element.attributes.id.value,
};
});
});
console.log(flats);
This part works like a charm with exact same class:
const flatsHashId = await newPage.evaluate(() =>
[...document.querySelectorAll(".result-item-v3")].map(
(elem) => elem.attributes.id.value
)
);
CodePudding user response:
Just change forEach
method to map
method. forEach
method doesn't return a new transformed array, instead it returns undefined
.
Like this:
const flats = await page.$$eval(".result-item-v3", (elements) => {
console.log(elements.length);
return elements.map((element) => {
return {
link: element.querySelector(".object-image-link").getAttribute("href"),
id: element.attributes.id.value,
};
});
});
console.log(flats);