有没有办法让puppeteer的waitUntil“networkidle"?只考虑 XHR (ajax) 请求? [英] Is there a way to get puppeteer's waitUntil "networkidle" to only consider XHR (ajax) requests?

查看:301
本文介绍了有没有办法让puppeteer的waitUntil“networkidle"?只考虑 XHR (ajax) 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 puppeteer 来评估我的测试应用程序中基于 javascript 的网页 HTML.

I am using puppeteer to evaluate the javascript-based HTML of web pages in my test app.

这是我用来确保加载所有数据的行:

This is the line I am using to make sure all the data is loaded:

await page.setRequestInterception(true);
page.on("request", (request) => {
  if (request.resourceType() === "image" || request.resourceType() === "font" || request.resourceType() === "media") {
    console.log("Request intercepted! ", request.url(), request.resourceType());
    request.abort();
  } else {
    request.continue();
  }
});
try {
  await page.goto(url, { waitUntil: ['networkidle0', 'load'], timeout: requestCounterMaxWaitMs });
} catch (e) {

}

这是等待 ajax 请求完成的最佳方式吗?

Is this the best way to wait for ajax requests to be completed?

感觉不错,但我不确定是否应该使用 networkidle0、networkidle1 等?

It feels right but I'm not sure if I should use networkidle0, networkidle1, etc?

推荐答案

您可以使用 pending-xhr-puppeteer,一个公开承诺等待所有未决 xhr 请求得到解决的库.

You can use pending-xhr-puppeteer, a lib that expose a promise awaiting that all the pending xhr requests are resolved.

像这样使用它:

const puppeteer = require('puppeteer');
const { PendingXHR } = require('pending-xhr-puppeteer');

const browser = await puppeteer.launch({
  headless: true,
  args,
});

const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://page-with-xhr`);
// Here all xhr requests are not finished
await pendingXHR.waitForAllXhrFinished();
// Here all xhr requests are finished

免责声明:我是pending-xhr-puppeteer的维护者

DISCLAIMER: I am the maintener of pending-xhr-puppeteer

这篇关于有没有办法让puppeteer的waitUntil“networkidle"?只考虑 XHR (ajax) 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆