繁体   English   中英

在if语句CasperJS之后执行操作

[英]Perform action after if statement CasperJS

我在连续检查页面属性更改时遇到问题,但是我做到了。 现在我的主要代码如下:

casper.thenOpen('pageurl', function() { 
    this.then(function () {
        function checkReload()
        {
            var cur = this.getElementAttribute("classname", "attr");
            if (cur == "target") {here
                return; // finished
            }
            else
            {
                this.echo(cur);
            }
            this.reload();
            this.wait(1, checkReload); // check again in a second
        }
        this.then(checkReload);         
    }).thenEvaluate(function(){
        this.click("#new_add");
        this.echo("done567");   
        this.then.waitForSelector(".cartGdList",
            function pass () {
                this.click("...");
                this.echo("done8");
                this.then(function () {
                    this.waitForSelector(".cart_s_Box",
                        function pass () {
                            this.click("#js_upFormBtn");
                            this.echo("step4");
                            var end = new Date().getTime();
                            var time = end - start;
                            this.echo('time: ' + time + 'ms*');
                        },
                        function fail () {
                            this.echo('fail');
                        }
                    );
                });
            },
            function fail () {
                this.echo('fail');
            }
        );
    });
});

在if语句可以正常执行if(cur == "target")我想执行以下代码:

this.click("#new_add");
this.echo("done567");   
this.then.waitForSelector(".cartGdList",
    function pass () {
        this.click("...");
        this.echo("done8");
        this.then(function () {
            this.waitForSelector(".cart_s_Box",
                function pass () {
                    this.click("#js_upFormBtn");
                    this.echo("step4");
                    var end = new Date().getTime();
                    var time = end - start;
                    this.echo('time: ' + time + 'ms*');
                },
                function fail () {
                    this.echo('fail');
                }
            );
        });
    },
    function fail () {
        this.echo('fail');
    }
);

我该怎么做,我试图将其放在thenEvaluate但这不起作用。

您有很多不必要的嵌套,但是首先要考虑的是。 您想在if -branch中执行一些代码。 好了,然后把代码放在if分枝。

casper.thenOpen('pageurl', function() { 
    this.then(function () {
        function checkReload()
        {
            var cur = this.getElementAttribute("classname", "attr");
            if (cur == "target") {
                this.click("#new_add");
                this.echo("done567");
                // ...
                return; // stop recursion
            }
            else
            {
                this.echo(cur);
            }
            this.reload();
            this.wait(1, checkReload); // check again in a second
        }
        this.then(checkReload);
        // ...

如果您不想太多缩进,也可以将代码放入函数中。

function myIfBranch(){
    this.click("#new_add");
    this.echo("done567");
    // ...
}

casper.thenOpen('pageurl', function() { 
    this.then(function () {
        function checkReload()
        {
            var cur = this.getElementAttribute("classname", "attr");
            if (cur == "target") {
                myIfBranch.call(this);
                return; // stop recursion
            }
            else
            {
                this.echo(cur);
            }
            this.reload();
            this.wait(1, checkReload); // check again in a second
        }
        this.then(checkReload);
        // ...

至于不必要的嵌套。 请记住, then*wait*函数实际上都是异步步进函数,并且它们都以相同的异步方式运行。 这是一个清理的示例:

function myIfBranch(){
    this.click("#new_add");
    this.echo("done567");   
    this.waitForSelector(".cartGdList",
        function pass () {
            this.click("...");
            this.echo("done8");
            this.waitForSelector(".cart_s_Box",
                function pass () {
                    this.click("#js_upFormBtn");
                    this.echo("step4");
                    var end = new Date().getTime();
                    var time = end - start;
                    this.echo('time: ' + time + 'ms*');
                },
                function fail () {
                    this.echo('fail');
                }
            );
        },
        function fail () {
            this.echo('fail');
        }
    );
}

casper.thenOpen('pageurl', function() {
    function checkReload() {
        var cur = this.getElementAttribute("classname", "attr");
        if (cur == "target") {
            myIfBranch.call(this);
            return; // finished
        }
        else
        {
            this.echo(cur);
        }
        this.reload();
        this.wait(1, checkReload); // check again in a second
    }
    this.then(checkReload);

请记住, this里面casper.evaluatecasper.thenEvaluate并不是指casper ,但window 根本就没有window.clickwindow.echowindow.then函数。 另外, this.then.waitForSelector(...)没有任何意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM