Chrome-remote-interface: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: unexpected server response (500)

Created on 26 Apr 2017  路  7Comments  路  Source: cyrus-and/chrome-remote-interface

I want to do something on several tabs, so here is my code:

const CDP = require('chrome-remote-interface');

var Tab = ( function()  {
      process : function(url){
            CDP.New({'url':url}, function (err, target) {               
                if (!err) {
                    CDP((client) => {
                        const {Network, Page, Runtime} = client;
                        Network.enable();
                        Page.enable();
                        Runtime.enable();
                                                Page.navigate({url: URL});
                                                     ....
                                                do something
                                        });
                                 }
                           });
        }
})();

function openUrl(url) {                 
        var tab = new Tab();
        tab.process(url);   
}
openUrl('http://github.com');
openUrl('http://google.com');

Then error:
(node:8700) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: unexpected server response (500)

image

The function openUrl() will be called when there are some post requests. Therefore, I use two lines of openUrl() to represent.

I think the error may be happened when

CDP( (client) =>{          
});

Is CDP() just allow being used once?
How can I deal with the muli-tab problem? I need to do something on different tabs asynchronously...

not an issue question

Most helpful comment

This works fine for me, can you try it?

const CDP = require('chrome-remote-interface');

var Tab = function () {
    return {
        process: function (url) {
            CDP.New({'url':url}, function (err, target) {
                console.log(target);
                if (!err) {
                    CDP({target}, (client) => {
                        const {Network, Page, Runtime} = client;
                        Network.enable();
                        Page.enable();
                        Runtime.enable();
                        Page.navigate({url: url});
                        console.log('do something');
                    });
                }
            });
        }
    };
};

function openUrl(url) {
    var tab = new Tab();
    tab.process(url);
}

openUrl('http://github.com');
openUrl('http://google.com');

All 7 comments

Can you run again with --trace-warnings and show me the stacktrace?

OK, I got it, that error comes from the fact that you attempt to attach two instances to the same tab, normally this cannot happen but here there's a race between CDP.New() and CDP() which attach to the most recent target.

The trick is to specify which tab you want to inspect:

CDP({target}, (client) => { /*...*/ });

As a side note, if you specify your URL in CDP.New() then you attach to that tab there's no guarantee that you'll be able to properly intercept all the events, I suggest to create a new empty tab (about:blank), attach to it, then manually navigate your URL when your're done setting up your events.

I try target and it did't work...the same error
here is the trace warnings:

(node:256) Error: unexpected server response (500)
    at ClientRequest._req.on (D:\Codes\chromeheadless\node_modules\ws\lib\WebSocket.js:651:26)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at HTTPParser.parserOnIncomingClient (_http_client.js:473:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
    at Socket.socketOnData (_http_client.js:362:20)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)

This works fine for me, can you try it?

const CDP = require('chrome-remote-interface');

var Tab = function () {
    return {
        process: function (url) {
            CDP.New({'url':url}, function (err, target) {
                console.log(target);
                if (!err) {
                    CDP({target}, (client) => {
                        const {Network, Page, Runtime} = client;
                        Network.enable();
                        Page.enable();
                        Runtime.enable();
                        Page.navigate({url: url});
                        console.log('do something');
                    });
                }
            });
        }
    };
};

function openUrl(url) {
    var tab = new Tab();
    tab.process(url);
}

openUrl('http://github.com');
openUrl('http://google.com');

Still error..><

{ description: '',
  devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:9222/devtools/page/d14e7069-d827-4639-b817-2216a49965d0',
  id: 'd14e7069-d827-4639-b817-2216a49965d0',
  title: '',
  type: 'page',
  url: 'http://github.com/',
  webSocketDebuggerUrl: 'ws://localhost:9222/devtools/page/d14e7069-d827-4639-b817-2216a49965d0' }
{ description: '',
  devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:9222/devtools/page/863c39f6-1ee8-47d1-9b3a-43532f8407c4',
  id: '863c39f6-1ee8-47d1-9b3a-43532f8407c4',
  title: '',
  type: 'page',
  url: 'http://google.com/',
  webSocketDebuggerUrl: 'ws://localhost:9222/devtools/page/863c39f6-1ee8-47d1-9b3a-43532f8407c4' }
do something
(node:5912) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: unexpected server response (500)

I reinstalled the package, then it works...don't know why..
Thanks a lot!

OK, that's because I suggested to use the target option which has been introduced in v0.20.0 while you were using an older release (which uses tab instead). You're welcome!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paambaati picture paambaati  路  7Comments

renschler picture renschler  路  5Comments

hbakhtiyor picture hbakhtiyor  路  7Comments

zirill picture zirill  路  6Comments

anzeznidarsic picture anzeznidarsic  路  8Comments