zoukankan      html  css  js  c++  java
  • phantomjs form提交

    phantomjs表单提交,其实就是对DOM就行操作(获取元素),在这里实现了动态传入各种参数

    不说了 直接上代码

     1 var page = require('webpage').create(),
     2 system = require('system'),fname;                                   
     3  
     4 var hostUrl = system.args[1];
     5 var resId = system.args[2];
     6 var fileName = system.args[3];
     7 console.log("访问地址:"+hostUrl);
     8 console.log("资源名称:"+resId);
     9 console.log("资源路径:"+fileName);
    10 console.log('请稍等,正在提交数据。。。');
    11 
    12 page.open(hostUrl, function (stat) {
    13     page.uploadFile('input[name=resFile]', fileName);
    14     page.evaluate(function (resId) {
    15         document.querySelector('input[name=resId]').value = resId;
    16         document.querySelector('input[name=resConf]').value = '{"resVer":"'+new Date().getTime()+'"}';
    17         document.querySelector('form').submit();
    18     },resId);
    19     phantom.exit();
    20 });

    这里会出现一个问题,如果上传的资源文件过大,时间过长,就会出现上传失败

    所以在这里要通过一个状态来判断是否上传完成,onResourceReceived资源收到后调用后方法

     1 var page = require('webpage').create(),
     2 system = require('system'),fname;                                   
     3  
     4 var hostUrl = system.args[1];
     5 var resId = system.args[2];
     6 var fileName = system.args[3];
     7 console.log("访问地址:"+hostUrl);
     8 console.log("资源名称:"+resId);
     9 console.log("资源路径:"+fileName);
    10 console.log('请稍等,正在提交数据。。。');
    11 
    12 page.open(hostUrl, function (stat) {
    13     page.uploadFile('input[name=resFile]', fileName);
    14     page.evaluate(function (resId) {
    15         document.querySelector('input[name=resId]').value = resId;
    16         document.querySelector('input[name=resConf]').value = '{"resVer":"'+new Date().getTime()+'"}';
    17         document.querySelector('form').submit();
    18     },resId);
    19 });
    20 page.onResourceReceived = function(response) {
    21      if(response.stage=='end' && response.url.indexOf('.json')>-1){
    22         phantom.exit();
    23      }
    24 };

    这里判断了stage状态结束,还有当前地址改变,完成了上传,就能结束当前操作

    通过phantomjs实现了前端不能实现的,在测试中效果很明显

  • 相关阅读:
    向量杂谈
    对widget使用WM_SetCallback
    群延迟与广义线性相位
    采样的频域表示
    Oracle 表的连接方式(2)-----HASH JOIN的基本机制1
    Oracle 表的连接方式(1)-----Nested loop join和 Sort merge join
    Oracle 表的访问方式(2)-----索引扫描
    Oracle 表的访问方式(1) ---全表扫描、通过ROWID访问表
    11g RAC R2 之Linux DNS 配置
    11g RAC r2 的启停命令概述1
  • 原文地址:https://www.cnblogs.com/Vipming/p/4235694.html
Copyright © 2011-2022 走看看