入门phantomjs

前言

  phantomjs是一个无界面浏览器,用来操作web页面的一个工具,比如登录,提交表单等等。

 语法框

//创建一个浏览器对象
var page = require('webpage').create();


//传参部分
//var = system = require('system');
//检查参数长度

//if (system.args.length === 1){}
//console.log('Usage: phantomfs iframe.js <some URL>');
// phantom.exit();}
//参数获取

//var address ;

//address = system.args[1];


//日志输出编码

phantom.outputEncoding="gbk";
//web页面打开操作
page.open("url",function(status){
   //输出页面标题
  console.log(page.title); 
  //截图...
page.render("f51.png")

if(status == "success"){

 //此处可以写js 代码了。注意,此处的代码是并行执行的,要用setTimeout处理,如果多次调用写多个,延迟时间 平行顺延...
setTimeout(function(){
  
 page.evaluate{
     //真正操作页面js代码的地方//好像不能用for指令,我没操作成功
   if ( typeof(jQuery) == "undefined" )
            {
                // 如果没有jquery,远程加载一个
           page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
   );
            }
    




};



},2000);


setTimeout(function(){
  
//截图
page.render("end10101010.png")

//退出...

phantom.exit(0);
console.log("Page failed to load.");

},4000);












}

else{
//截图
page.render("end10101010.png")

//退出...

phantom.exit(0);
console.log("Page failed to load.");
}





});

 

html元素遍历常用指令

#加载ifame,假设ID为contentframe

document.getElementById("contentframe").contentDocument

#加载ID

document.getElementById("row_3")

#加载元素

document.getElementsByTagName("td")  //返回值是元组

#支持连续操作

document.getElementById("contentframe").contentDocument.getElementById("row_3").getElementsByTagName("td")[0].getElementsByTagName("input")[0].click()

#获取值

document.getElementById("row_3").[innerText,innerHtml,value];赋值="..."

#jq常用遍历

$("#id...").find("元素,返回查找的元素以后的所有集合").children("元素子元素,返回数组")

#jq 操作iframe

$("iframe_id...").contents().find("").children("")

程序执行

443端口运行方法

phantomjs.exe    --ignore-ssl-errors=yes  hello.js  //heloo.js为语法框里的代码

总结:

参考资料

   百度,google...

 

posted @ 2017-08-14 14:29  放在垃圾桶里  阅读(240)  评论(0编辑  收藏  举报