PhantomJS的Phantom节点包等效代码

我很难转换这个代码,使它可以在节点服务器中使用。 所以这段代码被编写成在PhantomJS进程(即$:phantomjs index.js)中运行,但是我想使用程序包require(“phantom”)在节点服务器中运行它。 我有困难让这两个callback工作,但是。

page.onLoadFinished = function(status){ console.log("Load Finished"); }; page.onUrlChanged = function(){ console.log("URL Changed"); }; 

这是我试图控制整个情况的可悲尝试。

  phantom.create(['--ignore-ssl-errors=yes','--load-images=no']).then(function(ph) { console.log("here"); ph.createPage().then(function(page) { page.property('onResourceRequested', function(requestData, networkRequest) { console.log(requestData.url); }); page.open('https://example.com/login').then(function(status) { console.log(status); if (status !== 'success') { console.log("failed connection")} else { page.evaluate(function() { document.getElementById('email').value = "stuff"; document.getElementById('password').value = "things"; setTimeout(document.getElementsByTagName('button')[0].click(),5000); console.log("login attempt"); setTimeout(document.URL, 2000); }); page.onLoadFinished = function(status){ console.log("Load Finished"); }; page.onUrlChanged = function(){ console.log("url changed"); }; } }); }); }); 

此外,代码工作,并获取页面,并单击button,但问题是在幻像login后,我需要从下一页的数据,我将使用onUrlChanged和onLoadFinished做。

page.onLoadFinished和page.onUrlChanged是页面打开执行的callback函数,因此在打开url之前分配它们是有意义的。

订阅console.log和网页错误消息也是一个有用的习惯。

  var phantom = require('phantom'); phantom.create(['--ignore-ssl-errors=yes','--load-images=no']).then(function(ph) { console.log("here"); ph.createPage().then(function(page) { page.property('onError', function(msg, trace) { var msgStack = ['ERROR: ' + msg]; if (trace && trace.length) { msgStack.push('TRACE:'); trace.forEach(function(t) { msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : '')); }); } console.error(msgStack.join('\n')); }); page.property('onConsoleMessage', function(msg, lineNum, sourceId) { console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); }); page.property('onResourceRequested', function(requestData, networkRequest) { console.log(requestData.url); }); page.property('onLoadFinished', function(status) { console.log("Load Finished with status " + status); }); page.property('onUrlChanged', function(targetUrl) { console.log("URL changed to: " + targetUrl); }); page.open('https://example.com/login').then(function(status) { if (status !== 'success') { console.log("failed connection")} else { page.evaluate(function() { document.getElementById('email').value = "email"; document.getElementById('password').value = "password"; setTimeout(function(){ console.log("login attempt"); document.getElementsByTagName('button')[0].click(); }, 5000); }); }); } }); }); });