简体   繁体   中英

How do I loop through a puppeteer selector response?

So, with page.evaluate I could do:

await page.evaluate(function() {
   var links = document.querySelectorAll('a');
   for (var i = 0; i < links.length; i++) console.log(links[i].href);
});

However I would like to do this with page.$$ and I'm unsure how you would do this. I'm trying to do everything without the need for page.evaluate because it seems entirely unnecessary. Puppeteer has lots of cool gadgets I'm trying to get a grasp with.

The documentation for what you're looking for is here: https://github.com/puppeteer/puppeteer/blob/v5.4.1/docs/api.md#pageselector-1 You can take that example and adjust it to your needs.

It'd be:

const linksHrefs
    = await page.$$eval('a', links => links.map(link => link.getAttribute('href')));
console.log(linksHrefs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM