casperjs中getPageContent()方法及getHTML()方法的使用
2015-10-16 22:35
761 查看
getPageContent()
Signature: getPageContent()
Retrieves current page contents, dealing with exotic other content types than HTML:
var casper = require('casper').create();
casper.start().then(function() {
this.open('http://search.twitter.com/search.json?q=casperjs', {
method: 'get',
headers: {
'Accept': 'application/json'
}
});
});
casper.run(function() {
require('utils').dump(JSON.parse(this.getPageContent()));
this.exit();
});
getHTML()
Signature: getHTML([String selector, Boolean outer])
Retrieves HTML code from the current page. By default, it outputs the whole page HTML contents:
casper.start('http://www.google.fr/', function() {
this.echo(this.getHTML());
});
casper.run();casper.start('http://www.google.fr/', function() {
this.echo(this.getHTML());
});
casper.run();
<html>
<body>
<h1 id="foobar">Plop</h1>
</body>
</html>You can fetch those contents using:
casper.start('http://www.site.tld/', function() {
this.echo(this.getHTML('h1#foobar')); // => 'Plop'
});
The outer argument allows to retrieve the outer HTML contents of the matching element:
casper.start('http://www.site.tld/', function() {
this.echo(this.getHTML('h1#foobar', true)); // => '<h1 id="foobar">Plop</h1>'
});
getFormValues()
Signature: getFormValues(String selector)
New in version 1.0.
Retrieves a given form all of its field values:
casper.start('http://www.google.fr/', function() {
this.fill('form', {q: 'plop'}, false);
this.echo(this.getFormValues('form').q); // 'plop'
});
casper.run();
getGlobal()
Signature: getGlobal(String name)
Retrieves a global variable value within the remote DOM environment by its name. Basically, getGlobal(’foo’)
will retrieve the value of window.foo from the page:
casper.start('http://www.google.fr/', function() {
this.echo(this.getGlobal('innerWidth')); // 1024
});
casper.run();
Signature: getPageContent()
Retrieves current page contents, dealing with exotic other content types than HTML:
var casper = require('casper').create();
casper.start().then(function() {
this.open('http://search.twitter.com/search.json?q=casperjs', {
method: 'get',
headers: {
'Accept': 'application/json'
}
});
});
casper.run(function() {
require('utils').dump(JSON.parse(this.getPageContent()));
this.exit();
});
getHTML()
Signature: getHTML([String selector, Boolean outer])
Retrieves HTML code from the current page. By default, it outputs the whole page HTML contents:
casper.start('http://www.google.fr/', function() {
this.echo(this.getHTML());
});
casper.run();casper.start('http://www.google.fr/', function() {
this.echo(this.getHTML());
});
casper.run();
<html>
<body>
<h1 id="foobar">Plop</h1>
</body>
</html>You can fetch those contents using:
casper.start('http://www.site.tld/', function() {
this.echo(this.getHTML('h1#foobar')); // => 'Plop'
});
The outer argument allows to retrieve the outer HTML contents of the matching element:
casper.start('http://www.site.tld/', function() {
this.echo(this.getHTML('h1#foobar', true)); // => '<h1 id="foobar">Plop</h1>'
});
getFormValues()
Signature: getFormValues(String selector)
New in version 1.0.
Retrieves a given form all of its field values:
casper.start('http://www.google.fr/', function() {
this.fill('form', {q: 'plop'}, false);
this.echo(this.getFormValues('form').q); // 'plop'
});
casper.run();
getGlobal()
Signature: getGlobal(String name)
Retrieves a global variable value within the remote DOM environment by its name. Basically, getGlobal(’foo’)
will retrieve the value of window.foo from the page:
casper.start('http://www.google.fr/', function() {
this.echo(this.getGlobal('innerWidth')); // 1024
});
casper.run();
相关文章推荐
- 大量 D3.js 示例
- 利用JSP编程技术实现一个简单的购物车程序
- JSON的操作之js中json转换
- 使用Firebug或chrome-devToolBar深入学习javascript语言核心
- javascript的json对象与字符串之间的转换
- js ajax
- org.json.JSONException:End of input at character 0 of
- JSON文件处理
- JavaScript之命名空间模式 浅析
- 贪吃蛇
- 灯开关
- javascript本地对象、内置对象和宿主对象
- JSON跟JSONP的区别以及实战
- js基础知识总结篇
- toFixed 扩展方法 js保留小数(四舍五入)
- denounce函数:Javascript中如何应对高频触发事件
- 写一下JavaScript的笔记
- js插件化
- Js-格式化时间戳函数
- 常见的Javascript报错及解决方案