Casperjs Not returning Google Search link titles BUT Screenshot & Source Code test works

你说的曾经没有我的故事 提交于 2019-12-11 10:27:42

问题


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.

来源:https://stackoverflow.com/questions/36386601/casperjs-not-returning-google-search-link-titles-but-screenshot-source-code-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!