Hi,
I'm trying to do the following actions:
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.
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.
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.