下面的代码希望Phantom.js加载页面,单击按钮并等待5秒钟,然后返回页面的HTML代码。
问题:然而,使用setTimeout()创建5秒延迟会导致page.evaluate函数将null返回给回调函数,而不是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