Phantom.js中的setTimeout [英] setTimeout in Phantom.js

查看:696
本文介绍了Phantom.js中的setTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码要求Phantom.js加载页面,单击按钮并等待5秒钟,然后返回页面的HTML代码。

The code below wants Phantom.js to load the page, click on a button and wait for 5 seconds before returning the page's HTML code.

问题:但是使用 setTimeout()创建5秒延迟会导致
page.evaluate 函数将 null 返回到回调函数而不是HTML。

Problem: However using setTimeout() to create the 5 seconds delay causes the page.evaluate function to return null to the callback function instead of the 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() with Meteor.setTimeout()导致另一个错误:

Replacing setTimeout() with Meteor.setTimeout() causes another error:

phantom stdout: ReferenceError: Can't find variable: Meteor


推荐答案

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

page.evaluate() is the sandboxed page context of PhantomJS. It has no access to variables defined on the outside. If you need the timeout, then you need to do two calls to page.evaluate(), because you cannot return anything from a asynchronous function (explanation):

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

而不是使用第二个 page.evaluate()调用,您可以通过直接访问此处

Instead of using the second page.evaluate() call, you may shorten the code by directly accessing the content as defined here:

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

这篇关于Phantom.js中的setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆