嘿,我正在尝试使用 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();
关于javascript - 截取元素的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471933/