【问题标题】:Puppeteer blank page when accesing URL访问 URL 时的 Puppeteer 空白页面
【发布时间】:2021-12-09 02:20:46
【问题描述】:

所以我想去 drpciv.ro 获取一些信息。

代码如下:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.drpciv.ro/');
  await page.screenshot({path: 'example.png'});
    
  let texts = await page.evaluate(() => {

    const elements = Array.from(document.querySelectorAll('div.info-box-content'));
    return elements.map((div)=>div.innerText);
    
}); 
console.log(texts);
  await browser.close();
})();

当我输出变量 'texts' 时,控制台显示该变量为空并显示 [] 所以我假设站点加载了,因此找不到我要求的元素。 我尝试制作屏幕截图以查看页面是否加载并且它是空白的。 有什么帮助吗?

【问题讨论】:

    标签: javascript node.js puppeteer


    【解决方案1】:

    那是因为去到 url 并不一定意味着页面已经完全加载。而是尝试在 goto 选项中添加可选参数 waitUntil,以便应用程序仅在网络空闲指定时间后才解析此承诺。

    await page.goto('https://www.drpciv.ro', {
        waitUntil: 'networkidle0' 
    });
    

    详情请咨询official documentation page

    【讨论】:

      【解决方案2】:

      这发生在我身上。将 puppeteer 升级到最新版本后解决。 如果你的 puppeteer 版本太旧,那么 chrome 版本就会太旧,这意味着某些 js 方法可能不支持。

      【讨论】:

        猜你喜欢
        • 2019-10-23
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-07
        相关资源
        最近更新 更多