首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用PhantomJS提交表单

如何使用PhantomJS提交表单
EN

Stack Overflow用户
提问于 2012-02-12 12:23:30
回答 4查看 125.9K关注 0票数 161

我正在尝试使用phantomJS (顺便说一句,这是一个多么棒的工具!)为我拥有登录凭据的页面提交表单,然后将目标页面的内容输出到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="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();
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-02-13 13:42:29

我想通了。基本上这是一个异步问题。您不能只提交并期望立即呈现后续页面。您必须等到下一页的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);
票数 231
EN

Stack Overflow用户

发布于 2012-03-19 01:50:25

此外,CasperJS还为PhantomJS中的导航提供了一个很好的高级界面,包括单击链接和填写表单。

CasperJS

已更新以添加July 28, 2015 article comparing PhantomJS and CasperJS

(感谢评论者M先生!)

票数 62
EN

Stack Overflow用户

发布于 2013-02-04 23:53:07

发送原始POST请求有时会更方便。下面你可以看到来自PhantomJS的post.js original example

// 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();
});
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9246438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档