Chrome-remote-interface: CDP.Close() not working with Chrome 59 on macOS

Created on 6 Jul 2017  路  11Comments  路  Source: cyrus-and/chrome-remote-interface

Hi,
I'm trying to do the following actions:

  1. open a new (headless) Chrome tab
  2. navigate to an URL
  3. take e screenshot
  4. close Tab

And closing tab doesn't work. In fact, if I do a http://localhost:9222/json/list
all the tabs opened at specified pages are listed.

Basically, the code is the following:

...
const target = await CDP.New({ port: this.options.port || 9222 });
const tab = await CDP({ target });     
const tabId = tab.target.id;      
const { Page, Emulation } = tab;

await Promise.all([
  Page.enable(),
  Emulation.setDeviceMetricsOverride(options.metrics),
  Emulation.setVisibleSize({ width: options.metrics.width, height: options.metrics.height }),
  Emulation.forceViewport({ x: 0, y: 0, scale: 1 }),
]);
await Page.navigate({ url: url });
await Page.loadEventFired();      
let { data } = await Page.captureScreenshot({ format: 'png', fromSurface: true });

// do something with data... 
// close tab   

await CDP.Close( {id: tabId});

I've also tried using Runtime:

await tab.Runtime.evaluate({expression: 'window.close();'})

but again, tabs stay open.
Many thanks.

external issue

All 11 comments

It's Close (capital C) here:

await CDP.close({id: tabId});

You should get a TypeError: CDP.close is not a function but maybe you're swallowing the promise error?

Otherwise the code seems right to me.

yes, sorry... actually I'm using CDP.Close() in my code (just edited the example above).
CDP.Close() returns a {"domain":{"domain":null,"_events":{},"_eventsCount":1,"members":[]}}
But, again, doing a listing via /json/list or chrome-remote-interface list, all tabs opened are returned.

Further investigations: calling a http://localhost:9222/json/close/b25a1487-9df1-4eb2-b2b2-a85b787045b7 for a valid tab Id, service replies with a "Target is closing" but it doesn't actually close it.

What version of Chrome are you using?

Version 59.0.3071.115 (Official Build) (64-bit) on macOS "Sierra"

Possibly a known bug, it should work fine with Chrome >= 61 (not sure about 60).

True.
It seems a bug on Chrome for Mac. Running the same code on a Docker Container (Linux), tabs are closed as expected. Many thanks for the help.

You're welcome! :)

FYI for anybody else that lands here, the bug has been fixed in Chrome Canary: https://www.google.com/intl/en/chrome/browser/canary.html

@cyrus-and perhaps we should comment about this in the readme where appropriate? If you agree I'll open a PR.

@benjaminwood yeah I wanted to add a FAQ entry about the (hopefully transient) headless problems, nothing too specific though, I don't really want to keep track of all the upstream issues and update this README accordingly.

Yeah, that makes sense. I've opened a PR that adds a general note. Feel free to tweak it or let me know how you'd like it to change and I can adjust the PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sergueyarellano picture sergueyarellano  路  6Comments

renschler picture renschler  路  5Comments

anzeznidarsic picture anzeznidarsic  路  8Comments

westy92 picture westy92  路  7Comments

puppeteer701 picture puppeteer701  路  4Comments