我遇到了连续检查页面属性更改的问题,但我做到了。 现在我的主要代码如下:
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 statement is OK 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
上,但这不起作用。
答案 0 :(得分:0)
你有很多不必要的嵌套,但首先要做的事情。您想在if
- 分支中执行一些代码。好的,然后将代码放在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.evaluate
内的casper.thenEvaluate
并不是casper
,而是window
。根本没有window.click
,window.echo
或window.then
功能。此外,this.then.waitForSelector(...)
没有意义。