0

I'am visiting certain websites with phantomjs. Is it possible to run functions from sites environment in page.evaluate method? Can you provide and example of correct usage.

1

1 Answer 1

1

Yes, of course it is possible. You just call the site function inside of page.evaluate. Consider the example:

example.com html

<html>
    <head>
    </head>
    <body style="background-color: white">
    <p>A page</p>
    <script>
    function makeRed() {
        document.body.style.backgroundColor = "red";
    }
    </script>
    </body>
</html>

PhantomJS script

var page = require('webpage').create();
page.viewportSize = { width: 600, height: 300 };

page.open('http://example.com', function() {

    page.evaluate(function(){
        makeRed();
    });

    setTimeout(function(){
          page.render('red.png');
          phantom.exit();
    }, 1000);

});

Result:

Result

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.