0

I have been studying the usage of the puppeteer.connect method for almost an hour, but it still makes an error. Please help. I am a beginner. This method has troubled me for a long time.

const puppeteer = require('puppeteer');

(async ()=>{
    var wsa="ws://localhost:9222/devtools/browser/f59fe52c-d869-48a1-a7d4-c2b604a5b3";
    const browserConfig={
        browserWSEndpoint :wsa
    };
    const browser= await puppeteer.connect(browserConfig);
    const page=await browser.newPage();
    // todo 你的脚本内容
})().catch(err=>{
    console.log(err);
    process.exit();
});

The error that popped out

ErrorEvent {
  target: WebSocket {
    _events: [Object: null prototype] { open: [Function], error: [Function] },
  type: 'error',
  message: 'Unexpected server response: 404',
  error: Error: Unexpected server response: 404

    

Browser command line

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222

2 Answers2

0

I see other people's answers to solve the problem

 const puppeteer = require('puppeteer');

(async ()=>{
    const browserURL = 'http://127.0.0.1:9222';
    const browser = await puppeteer.connect({browserURL , defaultViewport : null  });
    const page = await browser.newPage();
    // todo 你的脚本内容
})().catch(err=>{
    console.log(err);
    process.exit();
});
  • If this solves your problem, please say how it did so too. A lone block of code won't be as much help. – user Sep 26 '21 at 18:46
0

Mainly, I made a mistake in the code before I modified it. Below is the official function explanation.

The browserWSEndpoint parameter I thought was entered in the chrome URL http://localhost:9222/json The content of webSocketDebuggerUrl inside , "ws://localhost:9222/devtools/page/F0B9E1C93A2F91F91F4A63D3FAEDDB"

As a result, you only need to enter the local ip plus the port number

But I still know the details after reading other people's code

puppeteer.connect(options)
options <Object>
browserWSEndpoint <string> a browser websocket endpoint to connect to.