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(() => {





Mmh ... but I need to somehow build the url with the value out of the array ...
– Philipp M
Aug 29 at 18:00





@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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)