本文介绍了如何将我的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需要= fresult.toFixed(3);

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

});

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

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

} else {
this.exit();
}

我遇到的问题超过了以下14个语句

  if(casper.exists()){

所以我试图做的是使用casperjs步骤作为函数。这是我在下面尝试过的,但它只是不做任何事情,casperjs在到达函数时结束。这是我正在尝试的

这是我所做的casperjs函数

 函数casperstep(金额,用户,位置){

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步骤不会触发。我怎样才能在理论上解决这个问题?它应该已经工作。



我一直在试着回答你的答案...



我的函数

p>

  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扩展来测试。

http://casperjs.readthedocs.org/en/latest/extending.html



so:

  casper.casperstep =函数(金额,用户,位置){
{您的指令....}
}

然后调用它:

  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();
}) ;

老猴补丁:)

要查看其他方法,请执行以下操作:


Okay, so here is a part of my casperjs script below which works fine

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();
}

The problem I have is over 14 of the following statements

if(casper.exists()){

So what I am trying to do, is use the casperjs steps as a function. This is what I have tried below, but it just does nothing and casperjs ends when it reaches the function. Here's what I am trying

This is the casperjs function I have made

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');
                    });
                });
            });
        });
    });
}

Then when I try the following

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

it just does not work at all. The casper steps just do not fire. How can I fix this in theory? It should have worked.

What I have been trying with your answers...

My function

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);
}

and how I am trying to use it

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

I tried the above to test out using casper extension.

解决方案

Well, you want to add your own method to a casper object instance : http://casperjs.readthedocs.org/en/latest/extending.html

so :

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

Then call it :

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();
});

Old-monkey patching :)

To see other ways to do it : Custom casperjs modules

这篇关于如何将我的casperjs脚本的一部分转换为函数,以便多次使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:18