admin管理员组

文章数量:1026380

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();

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();

本文标签: javascriptpuppeteer detach CDP sessions safely for pageclose()Stack Overflow