example.js
1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
phantom.injectJs('casper.js');
function q() {
document.querySelector('input[name="q"]').setAttribute('value', '%term%');
document.querySelector('form[name="f"]').submit();
}
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return [].map.call(links, function(e) {
return e.getAttribute('href');
});
}
var links = [];
var casper = new phantom.Casper({
logLevel: "debug",
verbose: true
})
.start('http://google.fr/')
.thenEvaluate(q, {
term: 'casper',
})
.then(function(self) {
links = self.evaluate(getLinks);
})
.thenEvaluate(q, {
term: 'homer',
})
.then(function(self) {
links = links.concat(self.evaluate(getLinks));
self.log("Click on 1st result link").click('h3.r a');
})
.then(function(self) {
self.debugPage();
})
.run(function(self) {
self.echo(JSON.stringify({
result: self.result,
links: links
}, null, ' '));
self.exit();
})
;