Node.js puppeteer - Use values from array in a loop to cycle through pages
Node.js puppeteer - Use values from array in a loop to cycle through pages
I'm using node.js and puppeteer to get some data. ... now I want to visit different pages based on inregular values from an array:
My array looks like this:
rows = [ RowDataPacket
id: 1,
value: 'L3',
name: 'Steve' ,
RowDataPacket
id: 2,
value: 'GU',
name: 'Peter' ,
RowDataPacket
id: 3,
value: 'M5',
name: 'John' ]
So the pages I want to visit are:
await page.goto('url/search?action=getname&name=L3', waitUntil: 'load');
await page.goto('url/search?action=getname&name=GU', waitUntil: 'load');
await page.goto('url/search?action=getname&name=M5', waitUntil: 'load');
The array is actually 200 values long ... so I thought of doing a loop like this:
await page.goto('url', waitUntil: 'load');
rows = [ RowDataPacket
id: 1,
value: 'L3',
name: 'Steve' ,
RowDataPacket
id: 2,
value: 'GU',
name: 'Peter' ,
RowDataPacket
id: 3,
value: 'M5',
name: 'John' ]
console.log(rows.length); // 3
for (let i=1; i < rows.length; i++)
await page.goto('url/search?action=getname&name='+value, waitUntil: 'load');
But how can I access the values ('value') from the array and use it in the loop accordingly?
2 Answers
2
I would put the browser.newPage();
inside the loop, like this:
browser.newPage();
const page =
for (let i=1; i < rows.length; i++)
page[i] = await browser.newPage();
await page[i].goto('url/search?action='+rows[i].name+'&name='+rows[i].value, waitUntil: 'load');
Then, in the same or other for:
const newPage = await page[i].evaluate(() => {
@PhilippM sure doing
rows[i].value
– Emeeus
Aug 29 at 18:08
rows[i].value
@PhilippM i've updated the answer
– Emeeus
Aug 29 at 18:11
I'm getting a error though: page[i] = await browser.newPage(); .... SyntaxError: await is only valid in async function
– Philipp M
Aug 29 at 19:07
I’m not sure what’s in the rows
array, as [ RowDataPacket {
is not valid JavaScript, but assuming that’s a stand-in for the actual data, and each row has a value
property, you can use simpler syntax:
rows
[ RowDataPacket {
value
for (const row of rows)
await page.goto(`url/search?action=getname&name=$row.value`);
If you want to create a new page each time, per Emeeus’s answer, you can wrap it in try
/finally
:
try
finally
for (const row of rows)
const page = await browser.newPage();
try
await page.goto(`url/search?action=getname&name=$row.value`);
finally
await page.close();
Notes:
for...of
for
waitUntil
load
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Mmh ... but I need to somehow build the url with the value out of the array ...
– Philipp M
Aug 29 at 18:00