Javascript 第二次在forloop中运行时的恶梦中断
我正在做一个网络机器人检查是一个网页加载正确的噩梦。我有不止一个页面需要检查每个程序的执行情况。我了解如何通过检查页面选择器来使用噩梦检查页面是否加载。当我只检查一页时,它是成功的。但当涉及到for循环时,请检查2页。它失败了。我需要使用“vo”模块,因为我发现执行多个噩梦时,我们需要使用vo 我使用我的调试器进行检查。第一个循环执行得很好。第二个是在噩梦中的“.then”时的中断Javascript 第二次在forloop中运行时的恶梦中断,javascript,node.js,nightmare,Javascript,Node.js,Nightmare,我正在做一个网络机器人检查是一个网页加载正确的噩梦。我有不止一个页面需要检查每个程序的执行情况。我了解如何通过检查页面选择器来使用噩梦检查页面是否加载。当我只检查一页时,它是成功的。但当涉及到for循环时,请检查2页。它失败了。我需要使用“vo”模块,因为我发现执行多个噩梦时,我们需要使用vo 我使用我的调试器进行检查。第一个循环执行得很好。第二个是在噩梦中的“.then”时的中断 var run_true = function*() { yield nightmare .g
var run_true = function*() {
yield nightmare
.goto('https://www.hkbn.net/personal/home/tc/landing')
.wait('#root') // Wait for landing page
.catch(() => {
console.log('T:Not Found')
})
.then(() => {
console.log('T:Landing page is loaded.');
})
yield nightmare.end();
return null;
}
var run_fail = function*() {
yield nightmare
.goto('https://www.hkbn.net/personal/home/tc/landing')
.wait('#rodfsdfdsffsfdsfdsfot') // Wait for landing page
.then(() => {
console.log('F:Landing page is loaded.');
})
.catch(() => {
console.log('F:Not Found')
})
yield nightmare.end();
return null;
}
var test = function*(){
for(var i = 0; i <2 ; i++){
if (i==0){
var x = yield vo(run_fail)(); //x is meaningless, just let it call the run fail
}else{
var y = yield vo(run_true)();
}
}
}
vo(test)();
var run\u true=函数*(){
屈服噩梦
后藤先生('https://www.hkbn.net/personal/home/tc/landing')
.wait('#root')//等待登录页
.catch(()=>{
console.log('T:notfound')
})
.然后(()=>{
log('T:登录页已加载');
})
结束();
返回null;
}
var run_fail=函数*(){
屈服噩梦
后藤先生('https://www.hkbn.net/personal/home/tc/landing')
.wait('#rodfsdsfsfdsft')//等待登录页
.然后(()=>{
日志('F:登录页已加载');
})
.catch(()=>{
console.log('F:未找到')
})
结束();
返回null;
}
var测试=函数*(){
对于(var i=0;i我可以建议用承诺重写它吗?您将摆脱很多麻烦。此外,当您有end()
调用时,这意味着噩梦对象将在链式运行后释放。这也意味着您无法在第二次循环运行中重用该对象。下面是开始的代码:
const Nightmare = require('nightmare');
const nightmare = Nightmare({ show: true });
var run_true = function() {
return nightmare
.goto('https://www.hkbn.net/personal/home/tc/landing')
.wait('#root') // Wait for landing page
.catch(() => {
console.log('T:Not Found')
})
.then(() => {
console.log('T:Landing page is loaded.');
});
}
var run_fail = function() {
return nightmare
.goto('https://www.hkbn.net/personal/home/tc/landing')
.wait('#rodfsdfdsffsfdsfdsfot') // Wait for landing page
.then(() => {
console.log('F:Landing page is loaded.');
})
.catch(() => {
console.log('F:Not Found')
});
}
var test = async function() {
for (var i = 0; i < 2; i++) {
try {
if (i == 0) {
await run_fail();
} else {
await run_true();
}
} catch (e) {
// do something with errors
}
}
}
test().catch(err => {
console.error(err); // do something with error
process.exit(1);
}).then(result => {
console.log('All processes finished');
process.exit(0);
});
const噩梦=require(“噩梦”);
常量噩梦=噩梦({show:true});
var run_true=函数(){
回归噩梦
后藤先生('https://www.hkbn.net/personal/home/tc/landing')
.wait('#root')//等待登录页
.catch(()=>{
console.log('T:notfound')
})
.然后(()=>{
log('T:登录页已加载');
});
}
var run_fail=函数(){
回归噩梦
后藤先生('https://www.hkbn.net/personal/home/tc/landing')
.wait('#rodfsdsfsfdsft')//等待登录页
.然后(()=>{
日志('F:登录页已加载');
})
.catch(()=>{
console.log('F:未找到')
});
}
var test=异步函数(){
对于(变量i=0;i<2;i++){
试一试{
如果(i==0){
等待运行_失败();
}否则{
等待运行_true();
}
}捕获(e){
//做一些有错误的事情
}
}
}
test().catch(错误=>{
console.error(err);//处理错误
过程。退出(1);
})。然后(结果=>{
console.log(“所有进程已完成”);
进程退出(0);
});