当以下构建运行时,我得到错误" error:评估失败:ReferenceError: chunk is not defined"作为控制台响应。在下一行中,我可以到达chunk[a]作为console.log,但为什么我不能到达它在frame.evaluate?
const puppeteer = require('puppeteer');
const chunk= ["google.com","facebook.com","gmail.com","stackoverflow.com"];
(async () => {
for(var a=0;a<chunk.length;a++){
const browser = await puppeteer.launch({headless:false})
const page = await browser.newPage();
const iframeHandle = await page.$("frame[name='main']");
const frame = await iframeHandle.contentFrame();
console.log(chunk[a]);
await frame.evaluate(() => {
if(document.querySelector("#content > table").innerHTML.indexOf(chunk[a]) > -1){
console.log(chunk[a]);
}
});
}
})()
result: google.com计算失败:ReferenceError: chunk is not defined
我移动了async内部的块,chunk is not defined
消失了。然而,这次发生了另一个关于您想要到达的元素的错误。我试图通过添加.waitForSelector
和删除page.$
来纠正这一点,但发生了第三个错误,我认为这是由于拼写错误的元素路径("frame[name='main']")
。您可以更改它并尝试使用下面的代码:
(async () => {
const chunk= ["google.com","facebook.com","gmail.com","stackoverflow.com"];
for(var a=0;a<chunk.length;a++){
const browser = await puppeteer.launch({headless:false})
const page = await browser.newPage();
const iframeHandle = await page.waitForSelector("frame[name='main']");
const frame = await iframeHandle.contentFrame();
console.log(chunk[a]);
await frame.evaluate(() => {
if(document.querySelector("#content > table").innerHTML.indexOf(chunk[a]) > -1){
console.log(chunk[a]);
}
});
}
})()