简体   繁体   中英

Quality of the DOM element screenshot using “captureSelector” in CasperJS

I'm using casperJS to try to get a screenshot of a DOM element using captureSelector , but the quality of the screenshot is really poor, but when capturing full screenshot using capture it works correctly, the quality is correct.

Can anyone help me with this issue? Thanks in advanced.

CasperJS script

var casper = require("casper").create({
  verbose: true,
  logLevel: 'debug',
  pageSettings: {
    loadImages: true,
    loadPlugins: false,
  }
});
// ***************************************************
var viewport_width = 1524;
var viewport_height = 768;
var ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"; 
var filename = "./screenshot_"+new Date().getTime() + "_" + viewport_width + "x" + viewport_height + "_desktop.jpg";
var url = "http://url-here.com";
var lang_header = "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3";
// ***************************************************
casper.on('remote.message', function(msg) {
  this.log('Remote message caught: ' + msg, "error");
});
casper.on("page.error", function(msg, trace) {
  this.log("Page Error: " + msg, "error");
});
casper.on('load.finished', function (status) {
  if (status !== 'success') {
    this.log("Failed to load page", "error");
  }
  else {
    this.log("The page has been loaded", "info");
  }
});
casper.start();
casper.thenOpen(url, function() {
  this.userAgent(ua);
  this.viewport(viewport_width, viewport_height).then(function() {
    this.wait(5000, function() {
      this.waitForSelector('#dom-element-id', function() {
        this.captureSelector(filename, '#dom-element-id', { quality: 100 });
      });
    });
  });
});
casper.run();

Even using the quality imgOptions parameter I get really poor quality screenshots.

this.captureSelector(filename, '#dom-element-id', { quality: 100 });

PhantomJS: version 1.9.7

CasperJS: version 1.0.2

PD: I didn't put the real urls because is an internal project. Sorry for the inconvenience.

I've updated the versions and it works now:

PhantomJS: version 1.9.8

CasperJS: version 1.1.0-beta3

Thanks for the help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM