CasperJS Ajax点击捕获

CasperJS Ajax click capture

本文关键字:Ajax CasperJS      更新时间:2023-09-26

我正在尝试抓取网页。站点上有一个ajax按钮(一个div),当点击它时,它会在页面上添加更多结果的列表(比如显示20多个结果)。我想点击3次。

使用下面的代码:

casper.then(function() {
        for(var i=1; i<=3; i++){
            casper.evaluate(function(){
                $("div.showMore").click();
                return true;
            })
            casper.wait(5000, function then(){
                this.capture('image.png');
            })
        }
})

,但它只是捕获页面,没有点击。我确信这段代码点击…

$("div.showMore").click();

我检查了它通过推/尝试通过chrome控制台。

我错过了什么?

JavaScript是疯狂的异步性质。我会详细说明你在一个功能中的点击到下一个功能。这不是最理想的解决方案,但最有可能的是,你的代码的工作方式是Casper在for循环之前或期间截取截图。所以您的更改没有被注册。试着把它拼成四个不同的casper函数,看看结果是什么。

casper.then(function() {
        this.evaluate(function(){
            $("div.showMore").click();
        });
casper.wait(3000, function() {
        this.evaluate(function(){
            $("div.showMore").click();
        });
casper.wait(3000, function() {
        this.evaluate(function(){
            $("div.showMore").click();
        });
casper.wait(5000, function then(){
            this.capture('image.png');
        });