useful-bronze
useful-bronze2y ago

How to abort request if page is redirecting?

I've tried multiple different approaches but to no avail:
page.on('response', async (response) => {
if (Number(response.status()) === 301 || Number(response.status()) === 302) {
// crawlerContext.request.cancel(); // not working
// response.cancel() // not working

// crawlerContext.request.abort(); // not working
// response.abort() // not working
}
}
page.on('response', async (response) => {
if (Number(response.status()) === 301 || Number(response.status()) === 302) {
// crawlerContext.request.cancel(); // not working
// response.cancel() // not working

// crawlerContext.request.abort(); // not working
// response.abort() // not working
}
}
1 Reply
ondro_k
ondro_k2y ago
Hi, try this: (from stackoverflow):
await page.route('**', async route => {
const response = await route.fetch({maxRedirects: 0})
if (Number(response.status()) === 301 || Number(response.status()) === 302) {
...
}
})
await page.route('**', async route => {
const response = await route.fetch({maxRedirects: 0})
if (Number(response.status()) === 301 || Number(response.status()) === 302) {
...
}
})
I think that, in your approach, when page.on('response', ...) is fired, the redirect has been already made, that's why you should use page.route - to catch it before it's sent.
Stack Overflow
How to set Playwright not automatically follow the redirect?
I want to open a website using Playwright, but I don't want to be automatically redirected. In some other web clients, they have parameter link follow=False to disable automatically following the

Did you find this page helpful?