获取元素的屏幕截图

Take screenshot of element

嘿,我正在尝试使用 phantomjs 渲染特定元素的屏幕截图。我正在为 nodejs 使用 phantomjs 桥:phantom

这是我目前得到的:

page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
var elementBounds = page.evaluate(function () {
    var clipRect = document.querySelector("#yw0").getBoundingClientRect();
    return {
        top: clipRect.top,
        left: clipRect.left,
        width: clipRect.width,
        height: clipRect.height
    };
});
var oldClipRect = page.clipRect;
page.clipRect = elementBounds;
page.render('element.png');
page.clipRect = oldClipRect;

不知何故无法正常工作我总是得到整个页面的屏幕截图!我做错了什么?

使用 NightmareJs 解决了它。那是我的代码:

var Nightmare = require('nightmare');
var Screenshot = require('nightmare-screenshot');
var nightmare = new Nightmare();
nightmare.goto('url');
nightmare.use(Screenshot.screenshotSelector('element.png', 'img[id="yw0"]'));
nightmare.run();