入门phantomjs

前言

  phantomjs是一个无界面浏览器,用来操做web页面的一个工具,好比登陆,提交表单等等。html

 语法框

//建立一个浏览器对象
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为contentframejquery

document.getElementById("contentframe").contentDocumentweb

#加载IDajax

document.getElementById("row_3")api

#加载元素数组

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

#支持连续操做工具

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

#获取值google

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...

相关文章
相关标签/搜索