SORU
12 ŞUBAT 2012, Pazar


Nasıl bir form PhantomJS kullanarak göndermek için

PhantomJS (ne muhteşem bir alet bu arada!) kullanmaya çalışıyorum oturum açma kimlik bilgileri var olan bir sayfa için bir form göndermek için, ve sonra çıktı stdout hedef sayfanın içeriğini. Formu erişim ve değerlerini başarılı bir şekilde ayarlamak mümkün phantom kullanıyorum, ama oldukça doğru sözdizimi form ve çıktı sonraki sayfanın içeriğini göndermek için ne olduğundan emin değilim. Ben şimdiye kadar ne var:

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

CEVAP
13 ŞUBAT 2012, PAZARTESİ


Anladım. Temelde uyumsuz bir konu. Sadece göndermek ve Sonraki sayfa hemen işlemek için bekleyemezsiniz. Sonraki sayfa için yüklendiğinde olay tetiklenir kadar beklemek zorunda. Benim kod aşağıda:

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

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • FASHTAG

    FASHTAG

    5 EYLÜL 2012
  • FrameCityJackal

    FrameCityJac

    4 Aralık 2010
  • Jucyber Tutoriais

    Jucyber Tuto

    8 EYLÜL 2009