点击function不执行Nightmare.js执行

我试图做一些nightmare ,我的工作几乎function。 问题是当我在evaluate()run()被调用之后尝试执行click()时,我遇到了一个问题。 在我运行这两个函数之后,我试图执行另一个单击来将自己移动到网站的另一部分,但不执行click()

在这一点上,我记下确定最新的问题,我有几个假设,也许这些function是asynchronous的,我试图click()时,callback还没有准备好,或者其中一个function结束de当前nightmare对象,我不dont有范围了。

 var Nightmare = require('nightmare'); //var nightmare = Nightmare({show:true}) var express = require('express'); var fs = require('fs'); var request = require('request'); var cheerio = require('cheerio'); var app = express(); var urlWeb = "someurl"; var selectCity = "#ddl_city"; var selectTheater = "#ddl_theater"; var enterBtn = "#btn_enter"; var mainSelector = "#aspnetForm"; var flagReady = true; new Nightmare({show:true}) .goto(urlWeb) .wait(selectCity) .select(selectCity, '19') .wait(8000) .select(selectTheater, '12') .wait(1000) .click(enterBtn) .wait(mainSelector) .evaluate(function(){ //returning HTML for cheerio return document.body.innerHTML; }) .run(function(err, nightmare){ if (err) return console.log(err); // Loading HTML body on jquery cheerio var $ = cheerio.load(nightmare); //Looping on each div for seccion de Carterla para Hoy $('.showtimeDaily').each(function(index, element){ //spanish title console.log($(this).find('h3').children().text()); //english title console.log($(this).find('h4').text()); //schedule for today console.log($(this).find('li').children().text() + " "); //img for movie console.log($(this).find('img').attr('src')); //show time data such as gender, lenght, language console.log($(this).find('.showtimeData').text()); var showtimeData = $(this).find('.showtimeData').text(); //console.log(JSON.stringify(showtimeData.replace(/\t|\n/g, ""))); }) console.log('Done!'); }) //*****here is wen I try to click***** .click('a[href="../showtimes/weekly.aspx"]'); 

我在asynchronouscallback方面遇到了问题,所以我所做的就是嵌套了噩梦对象的调用,以确保这些任务正在一个接一个地运行。 这是代码:

 nightmare .goto(urlWeb) .wait(selectCity) .select(selectCity, '19') .wait(8000) .select(selectTheater, '12') .wait(1000) .click(enterBtn) .wait(mainSelector) .evaluate(function(){ //returning HTML for cheerio return document.body.innerHTML; }) .then(function(body){ // Loading HTML body on jquery cheerio var $ = cheerio.load(body); //Looping on each div for seccion de Carterla para Hoy $('.showtimeDaily').each(function(index, element){ //spanish title console.log($(this).find('h3').children().text()); //english title console.log($(this).find('h4').text()); //schedule for today console.log($(this).find('li').children().text() + " "); //img for movie console.log($(this).find('img').attr('src')); //show time data such as gender, lenght, language console.log($(this).find('.showtimeData').text()); var showtimeData = $(this).find('.showtimeData').text(); //console.log(JSON.stringify(showtimeData.replace(/\t|\n/g, ""))); }) //**Here I call nightmare to run after the first call back is done***** nightmare .goto('') .wait('body') .title() .then(function(title){ console.log(title); }); console.log('Done!'); });