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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55236975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:31:25  来源:igfitidea点击:

How to reload page in Puppeteer?

javascriptnode.jschromiumpuppeteer

提问by glhe13

I would like to reload the page whenever the page doesn't load properly or encounters a problem. I tried page.reload()but it doesn't work.

每当页面无法正确加载或遇到问题时,我想重新加载页面。我试过了,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();
                }

            }

        }

It gives me this error:

它给了我这个错误:

    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)

Some links are unable to load successfully. When I refresh those pages manually, it works. So I hope there is a function or a method that can help me reload my page automatically when there is an error.

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

采纳答案by glhe13

I manage to solve it using a while loop.

我设法使用 while 循环来解决它。

for (let appUrl of appUrls) {
    var count = i++;

    while(true){
        try{

            await page.goto(appUrl);

            const appName = await page.$eval('div.det-name-int', div => div.innerText.trim());

            console.log('\n' + count);
            console.log('Name: ' , appName);

            break;

            } catch(e){
              console.log('\n' + count);
              console.log('ERROR');
              await page.reload(appUrl);

              continue;
            }

}

回答by David Green

This works for me:

这对我有用:

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

See Puppeteer docs for details: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagereloadoptions

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

回答by Grynets

You always can reload page via DOM, like this:

您始终可以通过 DOM 重新加载页面,如下所示:

await page.evaluate(() => {
   location.reload(true)
})

or here is a lot of wayshow you can reload page with browser JS via DOM

或者这里有很多方法可以通过 DOM 使用浏览器 JS 重新加载页面

Also, you can navigate your puppeteer back and forward. Like this:

此外,您可以前后导航您的木偶操作员。像这样:

await page.goBack();
await page.goForward();

回答by AshTyson

So after the comments, the following line makes the error.

因此,在评论之后,以下行出现错误。

ERROR Error: Error: failed to find element matching selector "div.det-name-int"

bacause Puppetteer has a browser callback. When it finds the element and calls the callback, and if the element doesn't exist it throws an error.

因为 Puppetteer 有一个浏览器回调。当它找到该元素并调用回调时,如果该元素不存在,则会引发错误。

Also, the page is reloaded. You're not doing anything after that. If you want to fetch the image after that. Use

此外,页面会重新加载。在那之后你什么都不做。如果你想在那之后获取图像。利用

await page.$eval('div.det-name-int', div => div.innerText.trim());

after the reload. Or you can have a while loop to continuously check whether the element exists. If it doesn't then refresh page and check again. This ensures you will always have content.

重装后。或者您可以使用 while 循环来不断检查元素是否存在。如果没有,则刷新页面并再次检查。这可确保您始终拥有内容。

But if your content is dynamically generated and not part of the DOM at the moment you read the page, then your code becomes useless. You might need to add a timeout then search the dom for the element.

但是,如果您的内容是动态生成的,并且在您阅读页面时不是 DOM 的一部分,那么您的代码将变得毫无用处。您可能需要添加超时,然后在 dom 中搜索该元素。