最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - puppeteer detach CDP sessions safely for page.close() - Stack Overflow

matteradmin9PV0评论

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 ?

Share Improve this question edited Nov 17, 2024 at 0:15 C.Unbay asked Nov 17, 2024 at 0:02 C.UnbayC.Unbay 2,8273 gold badges18 silver badges33 bronze badges 2
  • Please share your full code as a minimal reproducible example. – ggorlen Commented Nov 17, 2024 at 2:13
  • Have you tried await session.detach();, and only then await page.close(); or await browser.close();? This fixed it for me. – tuurtje11 Commented Nov 17, 2024 at 10:54
Add a comment  | 

1 Answer 1

Reset to default 1

await 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();
Post a comment

comment list (0)

  1. No comments so far