puppeteer: UnhandledPromiseRejectionWarning: Error: Request is already handled!
I trying use puppeteer to crawler but I have a problem when I call request to “Kick out login!”. someone can help me! thanks
`const puppeteer = require(“puppeteer”); ( async () => { const browser = await puppeteer.launch() const page = await browser.newPage() await page.setRequestInterception(true) await page.setUserAgent(process.env.USER_AGENT)
page.on('request', interceptedRequest => {
interceptedRequest.continue({
method: 'POST',
postData: 'account=' + process.env.EMAIL + '&password=' + process.env.PASSWORD,
headers: {
...interceptedRequest.headers(),
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': process.env.COOKIE_FAKE
}
})
})
const response = await page.goto(process.env.LOGIN_URL)
const body = await response.json()
const token = body.result.token
// return
if (body.result.statusCode !== 200) {
return
} else if (body.result.type === 'login') {
console.log('Login success!')
// do something
}
else if (body.result.type === 'overLogin') {
console.log('Kick out login!')
page.on('request', interceptedRequest => {
interceptedRequest.continue({
method: 'POST',
postData: 'account=' + process.env.EMAIL + '&token=' + token,
headers: {
...interceptedRequest.headers(),
'Cookie': process.env.COOKIE_FAKE
}
})
})
const response = await page.goto(process.env.LOGOUT_URL)
let body = await response.json()
if (body.result.statusCode !== 200) {
return
} else if (body.result.type === 'login') {
console.log('Login success!')
// do something
} else {
return
}
}
})()`
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 14
- Comments: 32 (1 by maintainers)
Same here, still waiting for a fix…
Same
Same
I have the same issue
Hi, this is still an open issue.
Puppeteer throws due to these lines:
HTTPRequest.js
line 217 ->assert_js_1.assert(!this._interceptionHandled, 'Request is already handled!');
line 268 ->assert_js_1.assert(!this._interceptionHandled, 'Request is already handled!');
line 314 ->assert_js_1.assert(!this._interceptionHandled, 'Request is already handled!');
Disabling these hot path assertions makes everything work perfectly. Why are these assertions even in the hot path? They seem like debugging code, or perhaps test code, that was left in production. Again, removing these assertions fixes everything … so if they’re not needed for everything to work, then please drop them from the src, as they’re causing issues and are observably not required.
Same, any update on this ?
Happens to me too, anyone has any idea why?
Same here, still waiting for a fix…
I have the same issue as well. I uninstalled all of my puppeteer-related packages and then reinstalled each and then it worked again for a short time. Then it stopped working again… ugh
Not sure why this works, but here is the solution that worked for me:
I’m using puppeteer extra for ad and resource blocking, and I needed the “request” event for minor things (basically avoid the full page to load just to quickly push some existing state, like this).
In my case, calling
page.removeAllListeners("request");
is a good workaround. It’s just a patch, but request event seems to be at a page scope (browser.listenerCount("request")
always return 0), and - as far as I noticed - when a new page is created, the ad and resource blockers are working again.I noticed that I could do evil stuff, like collecting the old callback with
(page as any).emitter.all.get("request")[0]
(I could even check if it exists usingpage.listenerCount("request") > 0
) to set it back later on, but then problem of context binding arises and it’s just too much hassle.It’s just much simpler to create a page, remove the existing listener, do my stuff, close the page and start over. Maybe this workaround could help someone with a use case similar to mine.
I believe this was fixed by the cooperative interception mode: see https://pptr.dev/guides/request-interception In the legacy mode, a request abort/continue can only happen once and if it happens more than once the error is thrown. If that is the case, pls check your code and see if you (or puppeteer extension modules) register multiple event handlers. If you believe there is still a bug, please file a new report with a repro.
Same error
The same error here as well.
Any further update on this issue please?
Not sure why too but it still working with puppeteer and puppeteer-extra with any plugins, TY! 🚀
For those who are facing this issue, it’s happening when you assign a listener to the same event more than twice, so you want to do is either unsubscribe your listener once you’re done with the listener’s code execution and only then assign a new listener:
Otherwise, you always can create a new page and never unsubscribe the listener, it’s up to you and to your use case, but the first option is preferable in the aspect of CPU/memory load minimization.