puppeteer / puppeteer

JavaScript API for Chrome and Firefox
https://pptr.dev
Apache License 2.0
88.43k stars 9.07k forks source link

get JSON response of a specific request as in the network tab of DevTools #5314

Closed trvhung closed 2 years ago

trvhung commented 4 years ago

Hi..I am new and I'd like to get the response of the request from twitter shown in the screenshot

The current puppeteer code is shown below. Could anybody show me how to modify it so that it will get the JSON response directly from the browser? Thanks. Here is a code as an example:

const puppeteer = require('puppeteer'); (async () => { // const browser = await puppeteer.launch(); const browser = await puppeteer.launch({ headless: false
}); const page = await browser.newPage(); await page.goto('https://twitter.com/login'); await page.waitFor(3000) await page.type('input[name="session[username_or_email]"]', 'username'); await page.waitFor(3000) await page.type('.js-password-field', 'password'); await page.waitFor(3000) await page.click('.EdgeButtom--medium'); await page.waitFor(3000) await page.goto('https://mobile.twitter.com/holidaytimeuk/status/1213629088671371267'); await page.waitFor(3000) const linkHandlers = await page.$x("//*[@id='react-root']/div/div/div/main/div/div/div/div/div/div[2]/div/section/div/div/div/div[1]/div/article/div/div[6]/div[2]/a"); if (linkHandlers.length > 0) { const [response] = await Promise.all([ page.waitForResponse(response => response.url().includes('https://api.twitter.com/2/timeline/liked_by.json')), linkHandlers[0].click() ]); const dataObj = await response.json(); console.log(dataObj); } else { throw new Error("Link not found"); } })();

Here is data i want to get tB1ja

nick-0101 commented 3 years ago

I have the same question. Did you find a solution?

Fritzsierra commented 3 years ago

You must add this code before await page.goto

page.on('response', async (response) => {
if (response.url()=="https://sunat....."){ console.log('XHR response received'); console.log(await response.json()); } });

await page.goto("http://sunat...")

stale[bot] commented 2 years ago

We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm it yet. It will be closed if no further activity occurs within the next 30 days.

stale[bot] commented 2 years ago

We are closing this issue. If the issue still persists in the latest version of Puppeteer, please reopen the issue and update the description. We will try our best to accomodate it!