Phantom.js中的setTimeout函数

11
下面的代码希望Phantom.js加载页面,单击按钮并等待5秒钟,然后返回页面的HTML代码。
问题:然而,使用setTimeout()创建5秒延迟会导致page.evaluate函数将null返回给回调函数,而不是HTML。
myUrl = 'http://www.google.com'

var phantom = Meteor.npmRequire('phantom')
phantom.create = Meteor.wrapAsync(phantom.create)
phantom.create( function(ph) {

    ph.createPage = Meteor.wrapAsync(ph.createPage)
    ph.createPage(function(page) {

        page.open = Meteor.wrapAsync(page.open)
        page.open(listingUrl, function(status) {
            console.log('Page loaded')

            page.evaluate = Meteor.wrapAsync(page.evaluate)
            page.evaluate(function() {

                // Find the button
                var element = document.querySelector( '.search-btn' );

                // create a mouse click event
                var event = document.createEvent( 'MouseEvents' );
                event.initMouseEvent( 'click', true, true, window, 1, 0, 0 );

                // send click to element
                element.dispatchEvent( event );

                // Give page time to process Click event
                setTimeout(function() {
                    // Return HTML code
                    return document.documentElement.outerHTML
                }, 5000)

            }, function(html) {

                // html is `null`
                doSomething()

            })
        })
    })
})

setTimeout()替换为Meteor.setTimeout()会引发另一个错误:
phantom stdout: ReferenceError: Can't find variable: Meteor
2个回答

9

page.evaluate()是PhantomJS的沙盒页面上下文。它无法访问在外部定义的变量。如果你需要超时,那么你需要进行两次page.evaluate()调用,因为你无法从异步函数中返回任何内容(解释):

page.evaluate(function() {
    ...
    element.dispatchEvent( event );
}, function() {
    setTimeout(function() {
        page.evaluate(function() {    
            return document.documentElement.outerHTML
        }, function(html) {
            doSomething()
        })
    }, 5000)
})

不必使用第二个page.evaluate()调用,你可以直接访问在这里定义的内容来缩短代码:

setTimeout(function() {
    page.get("content", function(content) {
        doSomething()
    })
}, 5000)

0

这不是一个很好的解决方案,但如果你只想处理按钮点击和表单提交时的页面更改,它可以工作。 只需在page.open()之外声明函数变量,然后稍后在内部分配页面评估函数。当页面重新加载并从按钮点击中更改后,onLoadFinished将被调用,然后您可以再次进行评估。

var loadInProgress = false,
jurl = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js',
page = require('webpage').create();

// declare variables outside page.open and assign them later inside
var evalPageFunc;

// assign callbacks which will be called by phantom
page.onLoadStarted = function() {
    loadInProgress = true;
    console.log('load started');
};
page.onLoadFinished = function() {
    loadInProgress = false;
    console.log('load finished');
    if (evalPageFunc) {
      // since the page has loaded we can safely evaluate it
      var mydata = evalPageFunc();
      console.log(mydata);
      if (!mydata.havemore) {
        phantom.exit();
        // or next url
      }
    }
};

page.open(url, function(status) {
  page.includeJs(jurl, function(){

    // define your page evaluating functions
    evalPageFunc = function(){
      return page.evaluate(function() {
        var datafromhtml = {}, havemoretoclick = true;
        // get your data and perform clicks if you want to
        // datafromhtml.somedata = $('stealme').text();
        // $("clickme").click();
        return {
          havemore: havemoretoclick,
          data: datafromhtml
        };
      });
    }
    var k = evalPageFunc();
  });
});

虽然不太美观,但它能正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接