LucianoGanga / simple-headless-chrome

Simple abstraction to use Chrome as a Headless Browser with Node JS
MIT License
217 stars 50 forks source link

Error: No inspectable targets #73

Closed Tin-Nguyen closed 7 years ago

Tin-Nguyen commented 7 years ago

I'm facing the issue sometimes on my API using simple-headless-chrome

2017-09-08 04:49:14.021, [log,ERROR,5a9ca759-572b-6484-60b3-9a912ae80a36, 5a9ca759-572b-6484-60b3-9a912ae80a36] message: No inspectable targets, stack: Error: No inspectable targets
    at defaultTarget (/rendering/node_modules/chrome-remote-interface/lib/chrome.js:45:23)
    at /rendering/node_modules/chrome-remote-interface/lib/chrome.js:228:32
    at Promise.linkTransaction (/rendering/node_modules/newrelic/lib/instrumentation/core/globals.js:164:23)
    at Promise.wrapped [as __NR_wrapper] (/rendering/node_modules/newrelic/lib/transaction/tracer/index.js:155:28)
    at wrappedHandler (/rendering/node_modules/newrelic/lib/instrumentation/core/globals.js:198:26)
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
    at process.wrappedFunction (/rendering/node_modules/newrelic/lib/transaction/tracer/index.js:250:51)

Can someone help to check?

Tin-Nguyen commented 7 years ago

It seems it's related to the issue on chrome-remote-interface component https://github.com/cyrus-and/chrome-remote-interface/issues/213

LucianoGanga commented 7 years ago

Under what context does that happens?

Tin-Nguyen commented 7 years ago

It works well locally, however, it shows the error on server with

OS: AWS Linux
Running Chrome via Docker
Tin-Nguyen commented 7 years ago

can you help to check the issue @LucianoGanga ?

Tin-Nguyen commented 7 years ago

It works well with my local Docker Chrome, but it doesn't work on the Docker Chrome on server. Any advises to fix it?

Tin-Nguyen commented 7 years ago

I resolved the issue, the root cause is explained at https://github.com/cyrus-and/chrome-remote-interface/issues/213#issuecomment-335517254.

I changed the source code to only connect to the remote chrome instance once time when starting the server, it resolved my issue. Close the issue now

fangjj commented 6 years ago

@Tin-Nguyen I hit the Error, but how to ensure only one remote chrome instance. like this?

const CDPGlobal =  {
  client: null,
  getClient: async () => {
    if (this.client) {
      return this.client;
    } else {
      const {host, port, } = setting;
      const chromeParams = {
        host,
        port,
      };
      this.client = await CDP(chromeParams);
      const {Network, Page, Runtime} = client;
      await Page.enable();
      await Runtime.enable();
      await Network.enable();
      await Network.setCacheDisabled({cacheDisabled: true});
      return this.client;
    }
  },
  close: async () => {
    if (this.client) {
      this.client = null;
      await this.client.close();
    }
  }
}

let html = '';
try {
    const client = await CDPGlobal.getClient();
    const {Page,} = client;
    await Page.navigate({url});
    await Page.loadEventFired();
    const result = await Runtime.evaluate({
      expression: 'document.documentElement.outerHTML'
    });
    html = result.result.value;
  }
} catch (err) {
  console.error(err);
  await CDPGlobal.close();
} finally {
 console.log(html);
}