3
votes

I am using PhantomJS to get positions of certain elements on the page, for instance iframes or objects. Presently I

page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", function() {
    page.evaluate(function() {
        // Select attr etc.. 
        position = $(this).position();
        offset = $(this).offset();
    });
});

I've tried adding $(window).load() to the formula but it still isn't returning the correct element position a lot of the time. Especially with iFrames and Objects. Perhaps they're positioned after the DOM has loaded? Either way does anyone have any ideas how I can improve or change method to get much more accurate positions?

Kind regards,

Fab

1
I've also tried using timeouts - Fabor

1 Answers

3
votes

What position is being returned? How is it incorrect?

A few things.

1) When page.evaluate is invoked the page has already loaded. That happens at page.open time. window.onload will never fire inside page.evaluate; it has already fired.

2) I don't know what this is within page.evaluate.

3) Within a PhantomJS context, there's no real reason to use jQuery. You're better off using standard constructs such as document.documentElement.scrollTop.

Try this:

page.evaluate(function() {
    console.log(document.documentElement.scrollTop);
    console.log(document.getElementById("my-frame").getBoundingClientRect());
});