Appreciate someone can help me with this problem I'm having.
Please see my image to understand further.
https://onedrive.live.com/redir?resid=F95DD828CA2E63D7!1326&authkey=!AEbavlKl38fBJYI&v=3&ithint=photo%2cjpg
I have a screenshot of the actual CasperJS html capture. It shows that casperjs correctly entered the field in google. The problem is
- CasperJS not calling my function getLinks >> links = this.evaluate(getLinks);. >> it returns null.
- I have tested the actual selector >> querySelectorAll('h3.r a') and it works in browser.
var casper = require('casper').create({ verbose: true, logLevel: 'debug', pageSettings: { userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' } });
var url = 'http://www.google.com/';
var fs = require('fs');
var links = [];
function getLinks() {
var links = document.querySelectorAll('h3.r a');
this.echo('Getting links now-----------------------------------------')
return Array.prototype.map.call(link, function(e) {
return e.innerText;
});
}
casper.start(url, function() {});
casper.then(function() {
this.fillSelectors('form#tsf', {
'input[name="q"]': 'Funny Man'
}, true);
this.echo('\n Filling is complete...');
})
casper.then(function() {
this.mouseEvent('click', 'input[name="btnK"]');
this.echo('Mouse Event Done...');
casper.capture('screenshot/google_search0.png');
});
casper.then(function() {
this.mouseEvent('click','button[name="btnG"]' );
this.echo('***************************************Done');
casper.capture('screenshot/google_search1.png');
});
casper.waitFor(function check() {
return this.evaluate(function() {
return document.querySelectorAll('#resultStats').length >= 1;
});
}, function then() { // step to execute when check() is ok
casper.capture('screenshot/google_search2.png');
links = this.evaluate(getLinks);
console.log('BOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOyeahhhhhhhh')
}, function timeout() { // step to execute if check has failed
casper.capture('screenshot/google_search3.png');
this.echo('search page load has failed. go see why********************');
});
casper.then(function() {
this.echo('++++++++++++++++++++++++++++++++++++++++++++++++++');
links = this.evaluate(getLinks);
});
casper.run(function() {
this.echo('Capturing screenshot...');
casper.capture('screenshot/google2.png');
fs.write('outputs/google_results4.html', this.getHTML(), 'w');
this.echo(links);
casper.exit();
});
Thanks very much.