How to render an html element using phantomjs

11,893

Solution 1

This might be completely wrong but the link that I provided in my comment does it like this:

Change

var clipRect = page.evaluate(function () { 
return document.querySelector(".span7 demo").getBoundingClientRect(); });

to:

var clipRect = document.querySelector(".span7 demo").getBoundingClientRect(); });

EDIT

Okay so I wanted to figure this one out and here's the code that works for me. I'm not familiar with the phantomjs api to use querySelector so I ended up using getElementsByClassName which is pretty much the same.

var page = require('webpage').create();

page.open("http://n1k0.github.io/casperjs/#phantom_Casper_captureSelector", function    (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
    } else {
        window.setTimeout(function () {
            //Heres the actual difference from your code...
            var bb = page.evaluate(function () { 
                return document.getElementsByClassName("span7 demo")[0].getBoundingClientRect(); 
            });

            page.clipRect = {
                top:    bb.top,
                left:   bb.left,
                width:  bb.width,
                height: bb.height
            };

            page.render('capture.png');
            phantom.exit();
        }, 200);
    }
});

Solution 2

You can easily capture an element with an ID too. Just replace getElementsByClassName("classname")[0] by document.getElementById('divID'). A full working example would be:

var page = require('webpage').create();

page.open("https://www.sejlar.com/maps.html", function (status) {
    if (status !== 'success') {
        console.log('Page not found');
    } 
    else {
        var p = page.evaluate(function () {
            return document.getElementById('map').getBoundingClientRect();
        });

        page.clipRect = {
            top:    p.top,
            left:   p.left,
            width:  p.width,
            height: p.height
        };

        page.render('screenshot.png');
        phantom.exit(); 
    }
});
Share:
11,893

Related videos on Youtube

Serjical Kafka
Author by

Serjical Kafka

Updated on October 08, 2022

Comments

  • Serjical Kafka
    Serjical Kafka over 1 year

    I want to save the image inside the div specified in the code. But using the code below i"m getting some other portion rendered. Is this the correct way to do it? I'm just a beginner in phantomjs. So Please help.

    var page = require('webpage').create();
    
    page.open("http://n1k0.github.io/casperjs/#phantom_Casper_captureSelector", function    (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
        } else {
    
            var clipRect = page.evaluate(function () { 
            return document.querySelector(".span7 demo").getBoundingClientRect(); });
            page.clipRect = {
                top:    clipRect.top,
                left:   clipRect.left,
                width:  clipRect.width,
                height: clipRect.height
            };
    
    
    
            window.setTimeout(function () {
                page.render('capture.png');
                phantom.exit();
            }, 200);
        }
    });
    
  • Serjical Kafka
    Serjical Kafka over 10 years
    It is showing an error now. TypeError: 'null' is not an object <evaluating 'document.queryselector<".span7 demo">.getBountingClientRect'>
  • Serjical Kafka
    Serjical Kafka over 10 years
    Using var clipRect = page.evaluate(function () { return document.querySelector(".span7 demo").getBoundingClientRect(); }); i'm getting the png output. But I'm not getting the div i want.
  • Daniel Figueroa
    Daniel Figueroa over 10 years
    Yeah, my answer before was just crap, I'm sorry for that. But I've provided you with an example that works for me now.
  • Naveen
    Naveen over 9 years
    I reused the code and i was trying this on www.google.com trying to extract div lga. It wouldnt work. Any thoughts ?