puppeteer: Access JSON response of a specific request as in the network tab of DevTools

后端 未结 1 880
粉色の甜心
粉色の甜心 2020-12-11 19:27

I\'d like to directly get the response of the last HTTP request shown in the screenshot.

The current puppeteer code is shown below. Could anybody show me how to modi

相关标签:
1条回答
  • 2020-12-11 19:46

    You can use page.waitForResponse to wait for the response and response.json to parse the response as JSON.

    Code

    Replace the await linkHandlers[0].click(); part by this:

    const [response] = await Promise.all([
        page.waitForResponse(response => response.url().includes('/gene/api/data/Enhancers')),
        linkHandlers[0].click()
    ]);
    const dataObj = await response.json();
    console.log(dataObj);
    

    This will first wait for the response (while in parallel making the click). After the response is detected the response is parsed as JSON. To get the response result as plain text (instead of parsing it), you can use response.text()

    0 讨论(0)
提交回复
热议问题