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

情到浓时终转凉″ 提交于 2019-12-01 22:50:57

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()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!