Chromeless: Take screenshot of an element

Created on 27 Jul 2017  路  14Comments  路  Source: prisma-archive/chromeless

The screenshot() method returns the full-page screenshot, is there any way to get the screenshot of a DIV or any other element?

API feature

Most helpful comment

That's a great idea @buremba! We should definitely make this easier in the future! Thanks for bringing this up.

How would you like the API to look like?

@stevenvachon: Please note that unlike NightmareJS which has been around for many years and therefore seen a lot of API iterations and improvements, Chromeless is still pretty new. So if you're missing a feature, please open an issue (or even better: create a PR). I think this would be more productive instead of these kinds of comments. 馃檹

All 14 comments

Is it really "full-page", or just viewport screenshot?

Oh, sorry, it's the viewport. It's possible to calculate the viewport of the DIV, create a screenshot with that viewport so that the result covers the DIV and then crop the screenshot but most of the screenshot tools support this feature and it would be great if this library can also support it.

This is basically a proxy to Chrome itself, so if Chrome doesn't have it, you're out of luck

It's possible to accomplish this with a bit of post-processing. If the aim of this library is to provide a proxy to Chrome headless, then you're right but if the aim is to provide a high-level library (it looks to be like that) on top of Chrome, this would be a good feature.

Nightmare has a clip argument. This library is still very immature.

Page.captureScreenshot protocol method has clip parameter which you can use it.

I did it in my API, here's the example of the capturing only DOM node.
https://api.letsvalidate.com/v1/thumbs?url=stackoverflow.com&selector=%23question-mini-list

Here's how I did it.

// ...

const {root: {nodeId: documentNodeId}} = await DOM.getDocument();
let nodeId;

try {
  const node = await DOM.querySelector({
    selector: options.selector,
    nodeId: documentNodeId,
  });

  nodeId = node.nodeId;
} catch(err) {
  console.error('Error while DOM querying', {err});
}

if (nodeId) {
  const {model} = await DOM.getBoxModel({nodeId});

  clip = {
    x: model.content[0],
    y: model.content[1],
    width: model.width,
    height: model.height,
    scale: 1
  };
}

// ...

const {data} = await Page.captureScreenshot({clip});

Verbose. I'll stick with Nightmare.

@hbakhtiyor it's a hosted service, we would like to host the API in our own servers / AWS account because of security considerations. We attach the private cookie information to the session in order to get screenshots of user dashboards. That's why I'm interested in this library.

Navalia has had this for a while as well: https://joelgriffith.github.io/navalia/Chrome/screenshot/

That's a great idea @buremba! We should definitely make this easier in the future! Thanks for bringing this up.

How would you like the API to look like?

@stevenvachon: Please note that unlike NightmareJS which has been around for many years and therefore seen a lot of API iterations and improvements, Chromeless is still pretty new. So if you're missing a feature, please open an issue (or even better: create a PR). I think this would be more productive instead of these kinds of comments. 馃檹

Implemented in #137

Thanks!

I'm opening the issue since the PR is not merged yet. You can close it once it's merged.

@adieuadieu Hi, Any chances, the unreleased functionalities like this and custom path for saving screenshot getting released soon

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevenvachon picture stevenvachon  路  4Comments

vanloc0301 picture vanloc0301  路  4Comments

just-boris picture just-boris  路  5Comments

queicherius picture queicherius  路  3Comments

abotkugyu picture abotkugyu  路  3Comments