Подтвердить что ты не робот

Как отправить форму с помощью PhantomJS

Я пытаюсь использовать phantomJS (какой замечательный инструмент btw!), чтобы отправить форму для страницы, на которой у меня есть учетные данные для входа, а затем вывести содержимое целевой страницы в stdout. Я могу получить доступ к форме и успешно установить ее значения с помощью phantom, но я не совсем уверен, какой правильный синтаксис должен отправить форму и вывести содержимое следующей страницы. Что я до сих пор:

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="[email protected]";
          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();
}
4b9b3361

Ответ 1

Я понял это. В основном это асинхронная проблема. Вы не можете просто отправить и ожидать немедленного отображения следующей страницы. Вам нужно подождать, пока не будет запущено событие 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);

Ответ 2

Кроме того, CasperJS обеспечивает приятный высокоуровневый интерфейс для навигации в PhantomJS, включая щелчок по ссылкам и заполнению форм.

CasperJS

Обновлено, чтобы добавить 28 июля 2015 г. статья, сравнивающая PhantomJS и CasperJS.

(Спасибо комментатору г-ну М!)

Ответ 3

Отправка необработанных POST-запросов иногда может быть более удобной. Ниже вы можете увидеть post.js оригинальный пример от PhantomJS

// 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();
});

Ответ 4

Как уже упоминалось выше 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();
  });
});