Skip to content

Instantly share code, notes, and snippets.

@justindisney
Created May 29, 2015 22:46
Show Gist options
  • Save justindisney/7914ab63981f4a75d191 to your computer and use it in GitHub Desktop.
Save justindisney/7914ab63981f4a75d191 to your computer and use it in GitHub Desktop.
Render a single DOM element using PhantomJS
/*
* renderDomElement.js
* An example of how to render a single DOM element using PhantomJS 2.0
*/
var system = require('system');
if (system.args.length != 4) {
console.log('Usage: phantomjs renderDomElement.js <url> <class-selector> <output-file>');
phantom.exit();
} else {
var page = new WebPage();
var url = system.args[1];
var selector = system.args[2];
var outputFile = system.args[3];
page.open(url, function (status) {
if (status == 'success') {
page.clipRect = page.evaluate(function (selector) {
return document.querySelector(selector).getBoundingClientRect();
}, selector);
page.render(outputFile);
}
phantom.exit();
});
}
@Avaray
Copy link

Avaray commented Oct 15, 2016

Simple and useful. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment