admin管理员组文章数量:1435771
I have this code snipper I use with puppeteer
to create CDP session.
let browser = await puppeteer.launch(options);
const page = await browser.newPage();
const session = await page.target().createCDPSession();
await session.send('Input.synthesizeScrollGesture', {
x: 100,
y: 200,
yDistance: -150
});
when I use page.close() as the scroll is happening, the whole program crashes with
node_modules\puppeteer-core\lib\cjs\puppeteer\common\CallbackRegistry.js:73
this._reject(callback, new Errors_js_1.TargetCloseError('Target closed'));
^
TargetCloseError: Protocol error (Input.synthesizeScrollGesture): Target closed
at CallbackRegistry.clear
or this error
cdp\CDPSession.js:64
return Promise.reject(new Errors_js_1.TargetCloseError(`Protocol error (${method}): Session closed. Most likely the ${this.#targetType} has been closed.`));
^
TargetCloseError: Protocol error (Network.getCookies): Session closed. Most likely the page has been closed.
at CdpCDPSession.send (
I have no way to prevent this, please help. I want to detach safely and close the page safely.
Indeed the real question is how to page.close()
safely without crash ?
I have this code snipper I use with puppeteer
to create CDP session.
let browser = await puppeteer.launch(options);
const page = await browser.newPage();
const session = await page.target().createCDPSession();
await session.send('Input.synthesizeScrollGesture', {
x: 100,
y: 200,
yDistance: -150
});
when I use page.close() as the scroll is happening, the whole program crashes with
node_modules\puppeteer-core\lib\cjs\puppeteer\common\CallbackRegistry.js:73
this._reject(callback, new Errors_js_1.TargetCloseError('Target closed'));
^
TargetCloseError: Protocol error (Input.synthesizeScrollGesture): Target closed
at CallbackRegistry.clear
or this error
cdp\CDPSession.js:64
return Promise.reject(new Errors_js_1.TargetCloseError(`Protocol error (${method}): Session closed. Most likely the ${this.#targetType} has been closed.`));
^
TargetCloseError: Protocol error (Network.getCookies): Session closed. Most likely the page has been closed.
at CdpCDPSession.send (
I have no way to prevent this, please help. I want to detach safely and close the page safely.
Indeed the real question is how to page.close()
safely without crash ?
1 Answer
Reset to default 1await session.detach()
fixed it for me.
Your code threw a TargetCloseError, so that means that Puppeteer tried to interact with a closed target. I think that Puppeteer doesn't close the CDPSession automatically. Read https://pptr.dev/api/puppeteer.cdpsession for more info.
let browser = await puppeteer.launch(options);
const page = await browser.newPage();
const session = await page.target().createCDPSession();
await session.send('Input.synthesizeScrollGesture', {
x: 100,
y: 200,
yDistance: -150
});
// Detach the CDPSession before closing the page
await session.detach();
await page.close();
await browser.close();
本文标签: javascriptpuppeteer detach CDP sessions safely for pageclose()Stack Overflow
版权声明:本文标题:javascript - puppeteer detach CDP sessions safely for page.close() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745642901a2667963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
await session.detach();
, and only thenawait page.close();
orawait browser.close();
? This fixed it for me. – tuurtje11 Commented Nov 17, 2024 at 10:54