PhantomJS的Phantom节点包等效代码

时间:2016-03-20 17:45:29

标签: javascript node.js phantomjs

我很难转换此代码,因为它可以在节点服务器中使用。所以这段代码编写为在PhantomJS进程中运行(即$:phantomjs index.js),但我想使用包require(" phantom")在节点服务器中运行它。然而,我无法让这两个回调工作。

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");
          };

        }
      });
    });
  });

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

1 个答案:

答案 0 :(得分:3)

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

从网页订阅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);
            });

          });

        }
      });
    });
  });