运行网站';phantomjs中的JavaScript函数

运行网站';phantomjs中的JavaScript函数,phantomjs,Phantomjs,我正在使用phantomjs访问某些网站。是否可以在page.evaluate方法中从站点环境运行函数?您能提供正确用法的示例吗。是的,当然可以。您只需在页面内调用site函数。评估。举个例子: example.com html <html> <head> </head> <body style="background-color: white"> <p>A page</p> <

我正在使用phantomjs访问某些网站。是否可以在page.evaluate方法中从站点环境运行函数?您能提供正确用法的示例吗。

是的,当然可以。您只需在
页面内调用site函数。评估
。举个例子:

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>
结果:


PSSSSST。。。您应该尝试使用木偶演员而不是幻影:
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);

});