I'm trying to use page.$$eval()
to get data from an <ul>
, store the values in two arrays and then iterate through the arrays to put the data into key/value pairs. I'm struggling with getting the data into the arrays.
The HTML looks like this:
<ul class="b-profile__sections">
<li class="b-profile__sections__item">
<span class="b-profile__sections__link">
<span class="b-profile__sections__count">585</span>
<span class="b-profile__sections__name"> Posts </span>
</span>
</li>
<li class="b-profile__sections__item">
<span class="b-profile__sections__link m-no-hover">
<span class="b-profile__sections__count">110.3K</span>
<span class="b-profile__sections__name"> Likes </span>
</span>
</li>
</ul>
My code:
let user = {};
let properties = [];
let values = [];
await page.$$eval('.b-profile__sections__name', (elems, properties) => elems.forEach(elem => {
properties.push(elem.innerText); // push result into array
}), properties) // array passed as argument to page.$$eval()
await page.$$eval('.b-profile__sections__count', (elems, values) => elems.forEach(elem => {
values.push(elem.innerText); // push result into array
}), values) // array passed as argument to page.$$eval()
for (let i = 0; i < properties.length; i++) {
user[properties[i]] = values[i] //iterate through arrays to make key/value pairs
}
Error:
Error: Evaluation failed: ReferenceError: values is not defined
at __puppeteer_evaluation_script__:2:37
at Array.forEach (<anonymous>)
at __puppeteer_evaluation_script__:1:19
or:
Error: Evaluation failed: TypeError: Cannot read property 'push' of undefined
at __puppeteer_evaluation_script__:2:44
at Array.forEach (<anonymous>)
at __puppeteer_evaluation_script__:1:27
I'm guessing it's to do with the browser (function passed to page.$$eval()
)/Node.js (the two arrays) context. Is there a better way?