Casperjs没有返回Google搜索链接标题但是屏幕截图&源代码测试工作

时间:2016-04-03 14:23:33

标签: javascript html phantomjs screen-scraping casperjs

感谢有人可以帮我解决这个问题。 请参阅我的图片以进一步了解。
https://onedrive.live.com/redir?resid=F95DD828CA2E63D7!1326&authkey=!AEbavlKl38fBJYI&v=3&ithint=photo%2cjpg

我有一个实际的CasperJS html捕获截图。它显示casperjs正确进入谷歌领域。问题是

  1. CasperJS没有调用我的函数getLinks>> links = this.evaluate(getLinks);. >>它返回null。
  2. 我测试了实际的选择器>> querySelectorAll('h3.r a'),它在浏览器中工作。
  3. var casper = require('casper')。create({     详细说明:是的,     logLevel:'debug',     pageSettings:{         userAgent:'Mozilla / 5.0(Windows NT 6.1)AppleWebKit / 537.36(KHTML,如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();
    });
    

    非常感谢。

0 个答案:

没有答案