您的位置:首页 > Web前端 > JavaScript

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: