Liens
|
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: { // The WebPage instance used by Casper will use these settings
userAgent: 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',
loadImages: false,
loadPlugins: false
},
clientScripts: [ // The scripts will be injected in remote DOM on every request
'includes/jquery.js'
]
});
|
User agent
|
casper.start();
casper.userAgent('Mozilla/5.0 (X11; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0');
|
Inject jQuery
|
this.page.injectJs('jquery.min.js');
|
|
casper.on('error', function(msg, trace) {
this.echo("error: " + msg + "\n" + JSON.stringify(trace), "ERROR");
this.exit();
});
casper.on("page.error", function(msg, trace) {
this.echo("page.error: " + msg + "\n" + JSON.stringify(trace), "WARNING");
});
casper.on("resource.error", function(resourceError) {
this.echo("resource.error: " + resourceError.errorString + "\nurl: " + resourceError.url, "COMMENT");
});
casper.on("resource.requested", function(resource) {
this.echo("res.req: " + JSON.stringify(resource));
});
|
Configures and starts Casper, then opens the provided url and optionally adds the step provided by the then argument.
|
casper.open('http://www.domaine.fr/post.php', {
method: 'post',
data: {
'standard_param': 'foo',
'nested_param[]': [
'Something',
'Something else'
]
}
});
|
Evaluates an expression in the current page DOM context.
|
var links = [];
casper.thenEvaluate(function(parameter1) {
var allTags = document.querySelectorAll('tag');
var firstTag = document.querySelector('tag');
var idTag = document.getElementById('id');
}, parameter1);
|
|
Les variables externes à evaluate ne sont pas accessibles directement, il faut les passer en paramètre du callback. |
Par défaut, les messages de log provenant de evaluate ne sont pas affichés.
Pour changer ce comportement, capturer l’événement remote.message et afficher le message.
|
casper.on('remote.message', function(message) {
this.echo(message);
});
|
Get links
|
casper.thenOpen(url, function() {
var links = this.evaluate(function() {
return Array.prototype.map.call(document.querySelectorAll('a'), function(link) {
return { href: link.getAttribute('href'), title: link.textContent };
});
});
this.each(links, function(self, link) {
console.log('url:', link.href);
console.log('title:', link.title);
});
});
|
|
// attend que la page soit chargée
casper.waitForUrl('https://www.domaine.fr/page.aspx');
|
Cookies
|
casper.thenOpen(url, function() {
this.evaluate(function() {
document.cookie = "key1=value1; expires=Fri, 1 Aug 2100 20:00:00 UTC; path=/";
document.cookie = "key2=value2; expires=Fri, 1 Aug 2100 20:00:00 UTC; path=/";
});
});
|
|
casperjs --cookies-file=cookies.txt test.js
|
Page content
|
var utils = require('utils');
casper.start(url, function() {
this.echo(this.getHTML());
// pour un contenu non-HTML
utils.dump(this.getPageContent());
});
casper.thenEvaluate(function() {
console.log(document.body.outerHTML);
});
|
Click
|
casper.thenClick('#Id');
casper.thenEvaluate(function() {
document.getElementById('Id').click();
});
|
Tableau / Array
map
|
// tableau.map ne fonctionne pas
var nouveauTableau = Array.prototype.map.call(tableau, function(element) {
return element + '!';
});
|
forEach / each
|
// tableau.forEach ne fonctionne pas
Array.prototype.forEach.call(tableau, function(element) {
console.log(element + '?');
});
casper.each(tableau, function(self, element) {
console.log(element + '?');
});
// eachThen est synchrone = pour chaque élément lance un then
casper.eachThen(tableau, function(response) {
console.log(response.data);
});
|
Écrire dans un fichier
|
var fs = require('fs');
fs.write('chemin/fichier.txt', 'contenu');
|
Télécharger
|
casper.download(url, 'chemin/fichier.txt');
|
|
casper.then(function download() {
var request = this.evaluate(function buildDownloadRequest() {
document.getElementById('__EVENTTARGET').value='$ctl00$xxx';
document.getElementById('__EVENTARGUMENT').value='';
var request = {};
var form = document.getElementById('form1');
form.onsubmit = function() {
var data = {};
for(i = 0; i < form.elements.length; i++) {
//console.log(form.elements[i].name + " = " + form.elements[i].value);
data[form.elements[i].name] = form.elements[i].value;
}
request.action = form.action;
request.data = data;
return false; // Stop form submission
}
document.getElementById('downloadButtonId').click();
return request;
});
this.download(request.action, 'ExportData.xlsx', 'POST', request.data);
});
|
Exemple
|
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
userAgent: 'Mozilla/5.0 (X11; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0'
}
});
casper.on('error', function(msg, trace) {
this.echo("error: " + msg + "\n" + JSON.stringify(trace), "ERROR");
this.exit();
});
casper.on("page.error", function(msg, trace) {
this.echo("page.error: " + msg + "\n" + JSON.stringify(trace), "WARNING");
});
casper.on("resource.error", function(resourceError) {
this.echo("resource.error: " + resourceError.errorString + "\nurl: " + resourceError.url, "COMMENT");
});
casper.start('http://www.domaine.fr/', function getHtmlPage() {
this.echo(this.getHTML());
});
casper.run();
|