gpt4 book ai didi

javascript - 如何在 Puppeteer 中重新加载页面?

转载 作者:搜寻专家 更新时间:2023-11-01 00:19:32 24 4
gpt4 key购买 nike

每当页面加载不正确或遇到问题时,我想重新加载页面。我尝试了 page.reload() 但它不起作用。

for(const sect of sections ){

// Now collect all the URLs
const appUrls = await page.$$eval('div.main > ul.app-list > li > div.app-info a.app-info-icon', links => links.map(link => link.href));

// Visit each URL one by one and collect the data
for (let appUrl of appUrls) {
var count = i++;
try{
await page.goto(appUrl);
const appName = await page.$eval('div.det-name-int', div => div.innerText.trim());
console.log('\n' + count);
console.log(appName);
} catch(e){
console.log('\n' + count);
console.log('ERROR', e);
await page.reload();
}

}

}

它给我这个错误:

    ERROR Error: Error: failed to find element matching selector "div.det-name-int"
at ElementHandle.$eval (C:\Users\Administrator\node_modules\puppeteer\lib\JS
Handle.js:418:13)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at ElementHandle.<anonymous> (C:\Users\Administrator\node_modules\puppeteer\
lib\helper.js:108:27)
at DOMWorld.$eval (C:\Users\Administrator\node_modules\puppeteer\lib\DOMWorl
d.js:149:21)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at Frame.<anonymous> (C:\Users\Administrator\node_modules\puppeteer\lib\help
er.js:108:27)
at Page.$eval (C:\Users\Administrator\node_modules\puppeteer\lib\Page.js:329
:29)
at Page.<anonymous> (C:\Users\Administrator\node_modules\puppeteer\lib\helpe
r.js:109:23)
at main (C:\Users\Administrator\Desktop\webscrape\text.js:35:43)
at process._tickCallback (internal/process/next_tick.js:68:7)

部分链接无法加载成功。当我手动刷新这些页面时,它起作用了。所以我希望有一个函数或方法可以帮助我在出现错误时自动重新加载我的页面。

最佳答案

这对我有用:

await page.reload({ waitUntil: ["networkidle0", "domcontentloaded"] });

有关详细信息,请参阅 Puppeteer 文档:https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagereloadoptions

关于javascript - 如何在 Puppeteer 中重新加载页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55236975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com