是否有任何puppeteer api /解决方法来获取页面中发送/接收的总字节数。例如下面的代码为我提供了所有时序统计信息。

await page.evaluate(() => JSON.stringify(window.performance.timing))


同样,有什么办法或解决方法,我可以获得页面中收到的总字节数/发送的总字节数。字节数应包括HTTP,Websocket,XHR请求/响应标头,正文。

最佳答案

通过使用

const perfEntries = JSON.parse(
  await page.evaluate(() => JSON.stringify(performance.getEntries()))
);

console.log(perfEntries);


您将获得所有请求的条目。添加所有请求的大小。

transferSize / encodedBodySize / decodedBodySize

{ name: 'https://www.google.com/',
    entryType: 'navigation',
    startTime: 0,
    duration: 7156.92000000854,
    initiatorType: 'navigation',
    nextHopProtocol: 'h2',
    workerStart: 0,
    redirectStart: 0,
    redirectEnd: 0,
    fetchStart: 1.7300000181421638,
    domainLookupStart: 1.7300000181421638,
    domainLookupEnd: 1.7300000181421638,
    connectStart: 1.7300000181421638,
    connectEnd: 1.7300000181421638,
    secureConnectionStart: 0,
    requestStart: 722.3000000230968,
    responseStart: 863.4900000179186,
    responseEnd: 948.6600000236649,
    transferSize: 64008,
    encodedBodySize: 63410,
    decodedBodySize: 216421,
    serverTiming: [],
    unloadEventStart: 0,
    unloadEventEnd: 0,
    domInteractive: 1416.8400000198744,
    domContentLoadedEventStart: 1416.880000004312,
    domContentLoadedEventEnd: 1424.5000000228174,
    domComplete: 7133.590000012191,
    loadEventStart: 7133.6149999988265,
    loadEventEnd: 7156.92000000854,
    type: 'navigate',
    redirectCount: 0 },...............................................]

09-25 20:32