在 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
分支中执行一些代码。好的,然后将代码放在 if
-branch.
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);
请记住 casper.evaluate
和 casper.thenEvaluate
中的 this
不是指 casper
,而是指 window
。根本就没有 window.click
、window.echo
或 window.then
函数。另外,this.then.waitForSelector(...)
没有意义。
我在连续检查页面的属性更改时遇到问题,但我做到了。 现在我的主要代码如下所示:
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
分支中执行一些代码。好的,然后将代码放在 if
-branch.
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);
请记住 casper.evaluate
和 casper.thenEvaluate
中的 this
不是指 casper
,而是指 window
。根本就没有 window.click
、window.echo
或 window.then
函数。另外,this.then.waitForSelector(...)
没有意义。