1

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

  1. CasperJS not calling my function getLinks >> links = this.evaluate(getLinks);. >> it returns null.
  2. 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.

Ming
  • 332
  • 4
  • 17
  • Typo: `Array.prototype.map.call(link, function(e) {` should be `Array.prototype.map.call(links, function(e) {` – Artjom B. Apr 03 '16 at 14:54
  • Thanks for pointing out. I tried it but it still doesn't work. It gives me null. – Ming Apr 04 '16 at 01:14
  • I tried with a similar script and it works. I wonder why is google to unpredictable. Are there any measures to increase the reliability of the script? – Ming Apr 04 '16 at 01:33
  • It gives you `null`, because there was an error in `getLinks`. I missed the most obvious one: `this` refers to `window` in the page context. There is no way to reference `casper` in the page context. It's a TypeError, because `this.echo` is not a function. – Artjom B. Apr 04 '16 at 06:14
  • Thank you very much sir. Works perfectly. – Ming Apr 04 '16 at 06:26
  • This is a duplicate of [Message does not appear when called from evaluate method](http://stackoverflow.com/questions/25135598/message-does-not-appear-when-called-from-evaluate-method) (I will close it in a few days as duplicate when my current close vote expires) – Artjom B. Apr 04 '16 at 18:10
  • Yeah. I saw that post as well. Its similar to this question. – Ming Apr 05 '16 at 00:49
  • I've edited my answer there so that it also targets your question directly. It's always a good idea to link related information together in this way, because it enables future readers to find their issue solved and maybe learn a bit more, so that they don't have to ask further questions. As long as your question is not negatively scored, it will not be automatically deleted. – Artjom B. Apr 05 '16 at 10:00

0 Answers0