Search code examples
node.jswindowsweb-scrapinggoogle-chrome-devtoolspuppeteer

Puppeteer getting 404 when connecting to chrome browser remote debugging link (localhost:9222), how to fix this?


I am using puppeteer on an existing browser window using this code:

    const browser = await puppeteer.connect({
      browserWSEndpoint: 'ws://localhost:9222'
    });

I have started a chrome window with debugging on using this command:

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

To confirm if debugging is working, I opened the link localhost:9222 in a chrome window, and it opens a blank page with no error.

But, running the puppeteer code to connect to this open instance of chrome browser gives the following error:

ErrorEvent {
  [Symbol(kTarget)]: WebSocket {
    _events: [Object: null prototype] { open: [Function], error: [Function] },
    _eventsCount: 2,
    _maxListeners: undefined,
    _binaryType: 'nodebuffer',
    _closeCode: 1006,
    _closeFrameReceived: false,
    _closeFrameSent: false,
    _closeMessage: <Buffer >,
    _closeTimer: null,
    _extensions: {},
    _paused: false,
    _protocol: '',
    _readyState: 3,
    _receiver: null,
    _sender: null,
    _socket: null,
    _bufferedAmount: 0,
    _isServer: false,
    _redirects: 0,
    _autoPong: true,
    _url: 'ws://localhost:9222/',
    _originalIpc: false,
    _originalSecure: false,
    _originalHostOrSocketPath: 'localhost:9222',
    _req: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 4,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: true,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: true,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      strictContentLength: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      _closed: true,
      socket: [Socket],
      _header: 'GET / HTTP/1.1\r\n' +
        'User-Agent: Puppeteer 22.8.0\r\n' +
        'Sec-WebSocket-Version: 13\r\n' +
        'Sec-WebSocket-Key: 6MiJ7ERwGlj6SdBdUtCMIQ==\r\n' +
        'Connection: Upgrade\r\n' +
        'Upgrade: websocket\r\n' +
        'Host: localhost:9222\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],
      agent: undefined,
      socketPath: undefined,
      method: 'GET',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      joinDuplicateHeaders: undefined,
      path: '/',
      _ended: true,
      res: [IncomingMessage],
      aborted: true,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'localhost',
      protocol: 'http:',
      [Symbol(shapeMode)]: false,
      [Symbol(kCapture)]: false,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype],
      [Symbol(errored)]: null,
      [Symbol(kHighWaterMark)]: 16384,
      [Symbol(kRejectNonStandardBodyWrites)]: false,
      [Symbol(kUniqueHeaders)]: null,
      [Symbol(kAborted)]: true,
      [Symbol(kError)]: undefined
    },
    [Symbol(shapeMode)]: false,
    [Symbol(kCapture)]: false
  },
  [Symbol(kType)]: 'error',
  [Symbol(kError)]: Error: Unexpected server response: 404
      at ClientRequest.<anonymous> (E:\Creativity\Linkedin Scraping\Scraper\node_modules\ws\lib\websocket.js:912:7)
      at ClientRequest.emit (node:events:518:28)
      at HTTPParser.parserOnIncomingClient (node:_http_client:698:27)
      at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17)
      at Socket.socketOnData (node:_http_client:540:22)
      at Socket.emit (node:events:518:28)
      at addChunk (node:internal/streams/readable:559:12)
      at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
      at Readable.push (node:internal/streams/readable:390:5)
      at TCP.onStreamRead (node:internal/stream_base_commons:190:23),
  [Symbol(kMessage)]: 'Unexpected server response: 404'
}

Can you help me understand what is causing the 404 error and how to succesfully establish remote debugging onto the chrome browser?

Further info: My OS is Windows 11.

I have tried using 127.0.0.1 instead and it resulted in the same behaviour: Puppeteer gives 404, but in chrome browser it opens succesfully


Solution

  • puppeteer.connect takes a ConnectOptions object where:

    browserURL is supposed to be in the format http://{$host}:{$port}

    therefore, the correct syntax (as alrady mentionned at answer) in your case would be

    const browser = await puppeteer.connect({
      browserURL: 'http://127.0.0.1:9222'
    })
    

    browserWSEndpoint would be supposed to be in the format ws://{$host}:{$port}/devtools/browser/{$some_long_id}. See Chrome DevTools Protocol for reference