161

私は phantomJS (なんと素晴らしいツールです!) を使用して、ログイン資格情報を持つページのフォームを送信し、宛先ページのコンテンツを stdout に出力しようとしています。ファントムを使用してフォームにアクセスし、その値を正常に設定することはできますが、フォームを送信して後続のページのコンテンツを出力するための正しい構文がよくわかりません。私がこれまでに持っているものは次のとおりです。

var page = new WebPage();
var url = phantom.args[0];

page.open(url, function (status) {

  if (status !== 'success') {
      console.log('Unable to access network');
  } else {

    console.log(page.evaluate(function () {

      var arr = document.getElementsByClassName("login-form");
      var i;

      for (i=0; i < arr.length; i++) {

        if (arr[i].getAttribute('method') == "POST") {
          arr[i].elements["email"].value="mylogin@somedomain.com";
          arr[i].elements["password"].value="mypassword";

          // This part doesn't seem to work. It returns the content
          // of the current page, not the content of the page after 
          // the submit has been executed. Am I correctly instrumenting
          // the submit in Phantom?
          arr[i].submit();
          return document.querySelectorAll('html')[0].outerHTML;
        }

      }

      return "failed :-(";

    }));
  }

  phantom.exit();
}
4

4 に答える 4

231

私はそれを考え出した。基本的には非同期の問題です。送信するだけで、後続のページがすぐにレンダリングされることを期待することはできません。次のページの onLoad イベントがトリガーされるまで待つ必要があります。私のコードは以下の通りです:

var page = new WebPage(), testindex = 0, loadInProgress = false;

page.onConsoleMessage = function(msg) {
  console.log(msg);
};

page.onLoadStarted = function() {
  loadInProgress = true;
  console.log("load started");
};

page.onLoadFinished = function() {
  loadInProgress = false;
  console.log("load finished");
};

var steps = [
  function() {
    //Load Login Page
    page.open("https://website.com/theformpage/");
  },
  function() {
    //Enter Credentials
    page.evaluate(function() {

      var arr = document.getElementsByClassName("login-form");
      var i;

      for (i=0; i < arr.length; i++) { 
        if (arr[i].getAttribute('method') == "POST") {

          arr[i].elements["email"].value="mylogin";
          arr[i].elements["password"].value="mypassword";
          return;
        }
      }
    });
  }, 
  function() {
    //Login
    page.evaluate(function() {
      var arr = document.getElementsByClassName("login-form");
      var i;

      for (i=0; i < arr.length; i++) {
        if (arr[i].getAttribute('method') == "POST") {
          arr[i].submit();
          return;
        }
      }

    });
  }, 
  function() {
    // Output content of page to stdout after form has been submitted
    page.evaluate(function() {
      console.log(document.querySelectorAll('html')[0].outerHTML);
    });
  }
];


interval = setInterval(function() {
  if (!loadInProgress && typeof steps[testindex] == "function") {
    console.log("step " + (testindex + 1));
    steps[testindex]();
    testindex++;
  }
  if (typeof steps[testindex] != "function") {
    console.log("test complete!");
    phantom.exit();
  }
}, 50);
于 2012-02-13T05:42:29.077 に答える
62

また、CasperJS は、リンクのクリックやフォームへの入力など、PhantomJS でのナビゲーションに優れた高レベル インターフェイスを提供します。

CasperJS

PhantomJS と CasperJS を比較する 2015 年 7 月 28 日の記事を追加して更新しました。

(コメンテーターMさんありがとう!)

于 2012-03-18T17:50:24.643 に答える
19

生の POST リクエストを送信する方が便利な場合があります。以下に、 PhantomJS の post.js の元の例を示します。

// Example using HTTP POST operation

var page = require('webpage').create(),
    server = 'http://posttestserver.com/post.php?dump',
    data = 'universe=expanding&answer=42';

page.open(server, 'post', data, function (status) {
    if (status !== 'success') {
        console.log('Unable to post!');
    } else {
        console.log(page.content);
    }
    phantom.exit();
});
于 2013-02-04T15:53:07.043 に答える
6

上で述べたように、 CasperJSはフォームに入力して送信するための最良のツールです。fill() 関数を使用してフォームに入力して送信する方法の最も簡単な例:

casper.start("http://example.com/login", function() {
//searches and fills the form with id="loginForm"
  this.fill('form#loginForm', {
    'login':    'admin',
    'password':    '12345678'
   }, true);
  this.evaluate(function(){
    //trigger click event on submit button
    document.querySelector('input[type="submit"]').click();
  });
});
于 2016-03-05T20:40:08.383 に答える