我正在使用 puppeteer 来评估我的测试应用程序中基于 javascript 的网页 HTML。
这是我用来确保加载所有数据的行:
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) {
}
这是最好的等待方式吗阿贾克斯请求要完成的?
感觉不错,但我不确定是否应该使用 networkidle0、networkidle1 等?
您可以使用待处理的 xhr-puppeteer https://github.com/jtassin/pending-xhr-puppeteer,一个公开承诺等待所有待处理的 xhr 请求得到解决的库。
像这样使用它:
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的维护者
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)