点击功能不执行Nightmare.js

时间:2016-10-04 01:49:07

标签: node.js nightmare

我试图用nightmare进行一些拼抢,我的工作几乎正常。问题是,当我在调用click()evaluate()后尝试执行run()时,我遇到了问题。在我运行这两个功能之后,我尝试执行另一次点击以将自己移动到网站的另一部分,但是没有执行click()

此时我注意到问题是什么,我几乎没有假设,也许这些函数是异步的,我在回调尚未准备就绪时尝试click()或其中一个函数结束当前nightmare对象,我不再有范围了。

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"]');

1 个答案:

答案 0 :(得分:1)

我遇到了异步回调的问题,所以我做的是我嵌套了对噩梦对象的调用,以确保任务一个接一个地运行。这是代码:

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!');


});