【问题标题】:define page evaluate outside of page open in phantomjs在 phantomjs 中打开的页面之外定义页面评估
【发布时间】:2014-03-14 20:39:59
【问题描述】:

在使用 phantomjs 打开网页的基本示例中,我们使用下面的代码来打开网页,并在函数中评估页面打开何时完成。

var page = require('webpage').create();
page.open('http://www.sample.com', function() {
  page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
    page.evaluate(function() {
           console.log(document.title);
    });
    phantom.exit()
  });
});

是让我们在 page.open 回调函数之外的函数中定义 page.evaluate 的任何方式,以便在我们需要时调用它,而不是在页面打开之后调用它

【问题讨论】:

    标签: javascript phantomjs


    【解决方案1】:

    不知道你到底是什么意思,但根据我从你的例子中了解到的情况,这可能会有所帮助:

    var page = require('webpage').create();
    // document is already initialized
    document.title = 'internal call';
    
    page.onConsoleMessage = function (msg, lineNum, sourceId) {
        console.log('PAGE\'S CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
    };
    
    var func = function () {
        console.log('Title: ', document.title);
    }
    
    // calling outside of the page.open:
    func();
    
    page.open('http://google.com/', function () {
        page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function () {
            // calling inside:
            page.evaluate(func);
            page.close();
            phantom.exit(0);
        });
    });
    

    还有一个关于 page.evaluate 函数的关于参数和闭包的注释

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2019-03-02
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      相关资源
      最近更新 更多