如何将我的casperjs脚本的一部分转换为函数,以便我可以多次使用它

时间:2014-03-23 18:22:41

标签: function phantomjs casperjs

好的,所以这里是我的casperjs脚本的一部分,可以正常工作

if(casper.exists(ac1)){

    var uel = "https://example.ws/send.html?f=1099817";
    this.thenOpen(uel, function() {
        casper.wait(10000, function() {    
            casper.then(function() {
                this.evaluate(function() {
                    var amount = 0.29
                    var result = amount * 0.019
                    var result2 = result.toFixed(6);
                    var fresult = amount - result2;
                    var needed = fresult.toFixed(3);

                    document.getElementById('account').value = 'ydfg028';
                    document.getElementsByName('data')[0].value = needed;

                });

                this.click("input#sbt.button[type='submit']");

                casper.wait(10000, function() {
                    casper.then(function() {
                        this.capture("filenadfgmedsfg.jpg");
                        var el2 = this.getHTML();
                        fs.write('results23.html', el2, 'w');
                    });
                });
            });
        });
    });

} else {
    this.exit();
}

我遇到的问题超过以下14条陈述

if(casper.exists()){

所以我想做的是使用casperjs步骤作为函数。这是我在下面尝试过的,但它只是做了什么,当casperjs到达函数时结束。这是我正在尝试的事情

这是我所做的casperjs功能

function casperstep(amount, user, location) {

    var uel = "https://example.ws/send.html?f=" + location;
    this.thenOpen(uel, function() {
        casper.wait(10000, function() {    
            casper.then(function() {
                this.evaluate(function() {

                    var result = amount * 0.019
                    var result2 = result.toFixed(6);
                    var fresult = amount - result2;
                    var needed = fresult.toFixed(3);

                    document.getElementById('account').value = user;
                    document.getElementsByName('data')[0].value = needed;

                });


                this.click("input#sbt.button[type='submit']");

                casper.wait(10000, function() {
                    casper.then(function() {
                        this.capture("filenadfgmedsfg.jpg");
                        var el2 = this.getHTML();
                        fs.write('results23.html', el2, 'w');
                    });
                });
            });
        });
    });
}

然后当我尝试以下

if(casper.exists(ac1)){
    casperstep(0.29, "username", "3245324");
}

它根本不起作用。 casper步骤就是不要开火。我怎么能在理论上解决这个问题?它应该有效。

我一直在尝试你的答案......

我的功能

casper.captchaget = function (selector) {
    var Loc = this.getHTML(selector, true).match(/src="(.*?)"/)[1];
    var Ilocation = 'https://perfectmoney.is' + Loc;
    var image = Loc;
    var imagesplit = image.split ('?');
    var split1 = imagesplit[1];
    var string = split1 + ".jpg";
    this.download(Ilocation, string);
}

以及我如何使用它

casper.then(function(){
    this.captchaget('img#cpt_img');//this.casperstep(0.29, "username", "3245324");
});

我尝试了以上测试使用casper扩展。

1 个答案:

答案 0 :(得分:2)

好吧,您想将自己的方法添加到casper对象实例:http://casperjs.readthedocs.org/en/latest/extending.html

所以:

casper.casperstep = function (amount, user, location) {
        {your instructions....}
}

然后叫它:

casper.start();
casper.then(function(){
    if(casper.exists(ac1)){
        casper.casperstep(0.29, "username", "3245324");//this.casperstep(0.29, "username", "3245324");
    }
})
.run(function() {
       test.done();
});

老猴子补丁:)

要了解其他方法:Custom casperjs modules