PhantomJS:提交表格 [英] PhantomJS: submit a form

查看:136
本文介绍了PhantomJS:提交表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在填写并使用PhantomJS提交表单,然后输出结果页面.问题是,我完全不知道此事是否正在提交.

I am filling out and submitting a form using PhantomJS and then outputting the resulting page. The thing is, I have no idea if this thing is being submitted at all.

我打印出结果页面,但它与原始页面相同.我不知道这是因为它重定向回还是我没有提交它,或者我需要等待更长的时间或.在实际的浏览器中,它发送一个GET并接收一个cookie,在最终收到最终结果(航班数据)之前,它用于发送更多的GETS.

I print the resulting page, but it's the same as the original page. I don't know if this is because it redirects back or I didn't submit it or I need to wait longer or or or. In a real browser it sends a GET and receives a cookie, which it uses to send more GETS before eventually receiving the final result - flight data.

我复制了此示例如何使用PhantomJS提交表单 ,使用不同的url和page.evaluate函数.

I copied this example How to submit a form using PhantomJS, using a diferent url and page.evaluate functions.

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("http://www.klm.com/travel/dk_da/index.htm");
  },
  function() {
    //Enter Credentials
    page.evaluate(function() {

                     $("#ebt-origin-place").val("CPH");
                    $("#ebt-destination-place").val("CDG");
                    $("#ebt-departure-date").val("1/5/2013");
                    $("#ebt-return-date").val("10/5/2013");

    });
  }, 
  function() {
    //Login
    page.evaluate(function() {

    $('#ebt-flightsearch-submit').click() ; 

     # also tried:
     # $('#ebt-flight-searchform').submit();   

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

推荐答案

感兴趣的站点很难抓取.我从美国KLM网站记录了HTTP流量,并得到了以下消息:

The site of interest is rather complicated to scrape. I logged the HTTP traffic from the US KLM site and got this:

GET/travel/us_en/apps/ebt/ebt_home.htm?name=on&ebt-origin-place=New+York+-+John+F.+Kennedy+International+%28JFK%29%2CNew+York&ebt -destination-place = Paris +-+ Charles + De + Gaulle + Airport +%28CDG%29%2C + France& c%5B0%5D.os = JFK& c%5B0%5D.ost = airport& c%5B0%5D. ds = CDG& c%5B0%5D.dst =机场& c%5B1%5D.os = CDG&c; c%5B1%5D.ost =机场& c%5B1%5D.ds = JFK& inboundDestinationLocationType =机场&重定向= no& chdQty = 0& infQty = 0& c%5B0%5D.dd = 2013-07-31& c%5B1%5D.dd = 2013-08-14& c%5B1%5D.format = dd%2Fmm% 2Fyyyy& flex = true& ebt-cabin-class = ECONOMY& adtQty = 1& goToPage =& cffcc = ECONOMY& sc = false HTTP/1.1

GET /travel/us_en/apps/ebt/ebt_home.htm?name=on&ebt-origin-place=New+York+-+John+F.+Kennedy+International+%28JFK%29%2CNew+York&ebt-destination-place=Paris+-+Charles+De+Gaulle+Airport+%28CDG%29%2C+France&c%5B0%5D.os=JFK&c%5B0%5D.ost=airport&c%5B0%5D.ds=CDG&c%5B0%5D.dst=airport&c%5B1%5D.os=CDG&c%5B1%5D.ost=airport&c%5B1%5D.ds=JFK&inboundDestinationLocationType=airport&redirect=no&chdQty=0&infQty=0&c%5B0%5D.dd=2013-07-31&c%5B1%5D.dd=2013-08-14&c%5B1%5D.format=dd%2Fmm%2Fyyyy&flex=true&ebt-cabin-class=ECONOMY&adtQty=1&goToPage=&cffcc=ECONOMY&sc=false HTTP/1.1

您为表单元素注入的值不是其服务器正在寻找的值.

Your injected values for the form elements are not what their server is looking for.

在page.evaluate()内部,您已被沙盒化,但是示例代码包含一个钩子,用于将沙盒控制台活动获取到外部控制台.对于其他调试,您还可以包括对象检查器等,但是必须将它们插入到页面或传递到validate()的部分代码中.

Inside page.evaluate(), you are sandboxed, but the sample code includes a hook to get sandboxed console activity onto the external console. For other debugging, you can also include object inspectors, etc., but they have to be injected into the page or part of the code passed into evaluate().

这篇关于PhantomJS:提交表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆