casperjs中getPageContent()方法及getHTML()方法的使用

Winterto1990發表於2015-10-16


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();








相關文章