Is there any way to use puppeteer
to get the redirects with the response body (if there are any) of the request?
I implemented the following code but I can't find a way to get the redirects...
const page = await browser.newPage()
page.on('request', (data) => console.log(data));
await page.on('response', response => {
const url = response.url();
response.buffer()
.then (
buffer => {
bufferString = buffer.toString();
},
error => {
console.log(error)
}
)
})
await page.goto('https://www.ford.com', {waitUntil: 'networkidle0'});
Just check response.status()
in your response
handler - it will be 3xx for redirects:
page.on('response', response => {
const status = response.status()
if ((status >= 300) && (status <= 399)) {
console.log('Redirect from', response.url(), 'to', response.headers()['location'])
}
})
(Redirects don't usually have anything interesting in the response body, so you don't probably want to call response.buffer()
for them.)
This should work for every type of redirection: server-side and client-side (meta-refresh, JS, etc)
const page = await browser.newPage()
const redirects = [];
const client = await page.target().createCDPSession();
await client.send('Network.enable');
await client.on('Network.requestWillBeSent', (e) => {
if (e.type !== "Document") {
return;
}
redirects.push(e.documentURL);
});
await page.goto('https://www.ford.com');
await page.waitForNavigation();
console.log(redirects);
Something like that.
You need to check the response url, because in the case of a redirect, multiple URLs could be called and I think only the first one is a 301.
Note that expect
in my code is from the chai
library, but that's optional, assertion could be done with something else.
const webpage = await browser.newPage();
webpage.once('response', response => {
if (response.url() === 'https://www.ford.com') {
expect(response.status()).equals(301);
}
})
await webpage.goto('https://www.ford.com');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With