你如何在 Puppeteer 中逐页单击每个 link?

How do you click every link in a page one by one in Puppeteer?

当我尝试按以下方式逐页单击每个页面时出现错误:

我试过使用 puppeteer 的 .click 函数,但它给了我错误: 错误:执行上下文被破坏,很可能是因为导航。

const aTags= await page.$$('#someId > a')
for (const aTag of aTags) {
   await aTag.click();
   //Do stuff
   page.goto(url); //this goes back to the initial page with the list of URLs
}

想一个接一个地点击链接return到上一页

好吧,如果您通过单击第一个 link 转到新页面,则无法单击其余页面...因为您已经不在 link 页面中了 只需将所有 links 收集到一个数组中...只需使用另一个函数打开 links

for (const aTag of aTags) {
    let  href = await page.evaluate(el => el.getAttribute('href'), aTags);
    await open_links(href);
}



async function open_links( url ){
  // open new tab with the url 
}