首页 > 解决方案 > CDP 过滤网络请求 - Puppteer

问题描述

我正在尝试使用 Puppteer 获取特定网络请求的时间。

有没有办法在 Puppteer 中过滤来自 Chrome DevTools 协议的请求,以便responseReceived只有在收到这个特定的网络响应时才会触发?

我这个任务的实际代码是:

const client = await page.target().createCDPSession();
await client.send('Network.enable');

// this method is deprecated and I don't know if it was the right thing to use ( https://chromedevtools.github.io/devtools-protocol/tot/Network#method-setRequestInterception )
await client.send('Network.setRequestInterception', {
    patterns: [
        {
            urlPattern: 'https://www.example.com/*task=customaction*',
            resourceType: 'XHR',
            interceptionStage: 'HeadersReceived'
        }
    ]
});

client.on('responseReceived', (requestId, loaderId, timestamp, type, response, frameId) => {
    console.log(`Debug data from: ${response.url} (${requestId})`)
    console.log('Timing:')
    console.log(response.timing)
    detach()
})

该文档建议使用 Fetch 作为Network.setRequestInterceptionhttps://chromedevtools.github.io/devtools-protocol/tot/Fetch)的替代品,但看起来 Fetch Domain 建议修改网络请求。

标签: google-chrome-devtoolspuppeteerchrome-devtools-protocol

解决方案


为时已晚,希望您已经解决了这个问题。但是,发布以便任何人都可以从中受益。

您可以使用此处显示的指标的帮助


推荐阅读