Javascript CasperJS:Recursive casper.open在第二次出现时使用ajax响应失败
我正在使用casperJS和slimerJS引擎获取视频,并通过ajax响应加载 步骤Javascript CasperJS:Recursive casper.open在第二次出现时使用ajax响应失败,javascript,ajax,recursion,casperjs,slimerjs,Javascript,Ajax,Recursion,Casperjs,Slimerjs,我正在使用casperJS和slimerJS引擎获取视频,并通过ajax响应加载 步骤 获取URL列表 针对列表中的每个url对casper.open的递归调用 每个casper.open调用都需要等待上一个casper.open发送ajax响应 前两点很有效。我对第三个有问题。 重要的是要理解,我不需要等到页面加载完成后再加载下一个casper.open。我只需要等待来自每个url的一个ajax响应。当我捕捉到这个响应时,我可以关闭这个url并打开下一个url 为此,递归循环从第一次出现开始。
// Some JS code...
function receiver(resource) {
if (resource.contentType == 'myContentType') {
sources.push(resource.url);// When catch good conteType, push in sources array
}
}
casper.on("resource.received", receiver); // Listen responses
// Some JS code...
// Login below
casper.then(function() {
this.evaluate(function(username, password) {
document.querySelector('#username').value = username;
document.querySelector('#password').value = password;
document.querySelector('#LoginSend').click();
}, username, password);
});
casper.then(function() {
this.wait(1000, function() {
this.waitFor(function() {
urls = this.evaluate(function() {
//Get and return all urls
});
return true;
});
});
});
casper.then(function() {
this.wait(1000, function() {
click_link();
});
});
function click_link() {
casper.then(function() {
this.wait(1000, function() {
this.open(address + urls[i]);// Open new url, i tried with theOpen, don't work too.
this.waitFor(function() {
return !!sources[i]; // Wait for the contentType is catch
}, function() {
sources.pop(); // Because resource.received has two staged
this.wait(2000, function() {
// Until we don't go to all url, recursive call
if (sources.length < urls.length) {
i++;
click_link();
}
});
});
});
});
}
// Some JS code...
//一些JS代码。。。
功能接收器(资源){
if(resource.contentType=='myContentType'){
sources.push(resource.url);//当捕获好的contentType时,推入sources数组
}
}
casper.on(“resource.received”,receiver);//倾听回应
//一些JS代码。。。
//在下面登录
casper.then(函数(){
此.evaluate(函数(用户名、密码){
document.querySelector('#username')。value=username;
document.querySelector('#password')。value=password;
document.querySelector(“#LoginSend”)。单击();
},用户名,密码);
});
casper.then(函数(){
this.wait(1000,函数(){
this.waitFor(函数(){
URL=this.evaluate(函数(){
//获取并返回所有URL
});
返回true;
});
});
});
casper.then(函数(){
this.wait(1000,函数(){
单击链接();
});
});
函数单击链接(){
casper.then(函数(){
this.wait(1000,函数(){
this.open(address+url[i]);//打开新的url,我用theOpen试过了,不起作用。
this.waitFor(函数(){
return!!sources[i];//等待contentType为catch
},函数(){
sources.pop();//因为resource.received有两个阶段
this.wait(2000,function(){
//直到我们不去所有的url,递归调用
if(sources.length
问题是第一个casper.open打开页面,等待捕获好的contentType并将好的url存储在我的数组源中。然后,下一个casper.open是call,程序将永远停留在wait中。这是因为contentType是永不捕获的。。。但它与上一页的类型相同。如果我先从第二页开始,它就行了。只有下一次出现的递归循环不起作用,第一次出现并不重要。此外,如果我在浏览器(chrome或firefox)中打开这些页面,效果会很好,他会捕捉到内容类型。因此,问题来自我的程序。
我有时在resource.received侦听器期间出错:“错误类型$.cookie”。但即使我有jquery.cookie.js,它也不起作用 我试图关闭页面或存储cookie,并在每个casper.open之间清除页面,但仍然无法工作
提前谢谢。您使用哪个Slimer.js版本?请注册到
resource.error
,page.error
,remote.message
和casper.page.onResourceTimeout
事件()。可能有错误。Slimerjs 0.9.6,我之前已经把侦听器放在上面了,没有错误,但是今晚我会再试一次,如果我没有发现问题,请告诉你。谢谢你的回答。我之前没有时间检查,很抱歉。我的程序中已经有了所有这些事件侦听器。他们没有给我错误。正如我在第一篇文章中所说,收到一个文件时出现了一个错误:“$.cookie不是函数”。。。但即使我有jquery.cookie.js,它也不能工作。事实上,我只是发现了它。我在clientScripts选项中包括了“jquery”和“jquery.cookie.js”。这就是问题所在…:)谢谢你。