PhantomJS的Phantom节点包等效代码

Phantom node package equivalent code for PhantomJS

本文关键字:代码 节点 Phantom PhantomJS      更新时间:2023-09-26

我很难转换此代码以使其可在节点服务器中使用。所以这段代码被编写为在 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 和 onLoadFinish 来做。

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

订阅控制台.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);
            });
          });
        }
      });
    });
  });