Home > Mobile >  Open and refresh simultaneous a list of href puppeteer
Open and refresh simultaneous a list of href puppeteer

Time:07-10

I have a list of href in a first page :

const hrefsToVisit = await pageHome.$$('.seemore');
for( let hrefToVisit of hrefsToVisit ) {
    var linkToPage = await pageHome.evaluate(el => el.getAttribute("href"), hrefToVisit);
    console.log('link to visit : ', linkToPage);        
}

I want to open all this href, and refresh them continuously, and simultaneously. The ideal would probably be to open them each one in a tab in one dedicated "browser", and ask for refresh all tabs each 10 seconds for example, and do a scrap treatment for all pages after that.

But the problem is, I don't know how much tabs I will have, so I can't "name" the pages.

Like :

const hrefsToVisit = await pageHome.$$('.seemore');
for( let hrefToVisit of hrefsToVisit ) {
    var linkToPage = await pageHome.evaluate(el => el.getAttribute("href"), hrefToVisit);
    console.log('link to visit : ', linkToPage);  
    var tab1 = await browser2.newPage();   
    await tab1.goto(linkToPage, {waitUntil: 'domcontentloaded'});  
    
}

After that, in my "browser2", I will only have one tab, the last page analysed. I want to open a new page for each link, without create a "tabX" named variable, and then ask to refresh all tabs, and scrap all tabs content.

CodePudding user response:

Thanks ggorlen !

//In my first tab I have my home page with all links to scrap, I open one tab by link
for( let hrefToVisit of hrefsToVisit ) {
   var linkToDeal = await tabs[0].evaluate(el => el.getAttribute("href"), hrefToVisit);     
   tabs.push(await browser.newPage());
}
    
//Then I refresh all tabs in the same time
var promises = [];
for (let tab of tabs ) {
   promises.push(tab.reload({ waitUntil: ["domcontentloaded"] }));
}
    
await Promise.all(promises);
  • Related