gpt4 book ai didi

javascript - CasperJS WaitFor 不等待条件完成

转载 作者:行者123 更新时间:2023-11-28 20:42:17 25 4
gpt4 key购买 nike

我对casperJS比较陌生。我有一个脚本,我想在页面B中执行操作(即验证用户的确认邮件),然后在页面A中继续执行。代码片段如下

casper.waitFor(function(){
return this.run(function(){
return verifyEmail(user_details['email']);
});
},function then(){
this.wait(60000, function() {
this.reload(function(){
this.echo("Refresh");
this.capture('after-reload-a.png');
});
});
});

函数verifyEmail定义如下:

function verifyEmail(email){
return casper.open('someURL').then(function(response){
//extract URL from response
this.echo("URL"+url);
casper.start().thenOpen(url, function() {
this.waitForText('someText',function(){
this.capture("final.jpg");
});
});
return url;
});
};

在执行过程中,Casper 从不执行函数 verifyEmail(从不打印 URL)并继续执行 then() 函数。我在这里缺少什么?

最佳答案

对于脚本中的每个 casper 实例,您应该只使用一次 startrun 函数。由于 CasperJS 的执行是异步的,因此您不能返回任何东西。你必须等待它。

您可以使用第二个 casper 实例来设置全局变量,并在前一个实例中使用 waitFor 来等待它发生变化,如 here 所示。 .

根据您从响应中提取 URL 的方式,使用 __utils__.sendAJAX(someURL) 可能更容易在页面上下文中(通过 casper.evaluate)。请注意,这不应在 casper.waitFor 中完成,因为它具有默认的同步模式,无需等待。此外,waitFor 的第一个回调函数经常被调用以检查值是否更改,因此不应该那样使用。

第三种可能性是仅使用一个实例进行简单导航。似乎您在解析其他页面后重新加载了原始页面。为什么您通常不使用 thenOpen 访问 'someURL',进行解析然后使用 thenOpen 打开原始 URL 而无需任何等待?你甚至可以get the current URL并将其保存为(半)全局变量,以确保您返回到正确的页面。

关于javascript - CasperJS WaitFor 不等待条件完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28060096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com