2017-04-06 105 views
0

考慮以下NightmareJS腳本...NightmareJS腳本永不退貨?

var Nightmare = require('nightmare'); 

var yandex = new Nightmare() 
    .viewport(1000,1000) 
    .useragent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36") 
    .goto('https://yandex.com/') 
    .run(function(err, nightmare) { 
     if(err) { 
      console.log(err); 
     } 
    }); 

它只是掛起並永遠不會返回我的提示。操作被執行。我可以截圖並執行其他操作,但節點進程永遠不會結束。

回答

1

如果你使用那樣的惡夢,你將會遇到麻煩。使用Nightmare.js

async function test(url){ 
    try{ 
    const nightmare = Nightmare({show:true}) 
    await nightmare.useragent(userAgentOption) 
    const response = await nightmare.goto(url) 
    await nightmare.wait(2000) 
    const evaluated = await nightmare.evaluate(()=>{ 
     return document.querySelector("input").innerHTML 
    }) 
    }catch(err){ 
    throw new Error(err)  
    } 
} 

當異步/等待解決了很多問題,你可以在每一步後登錄的功能裏面看到它失敗。

更好的選擇是使用WebStorm因爲它工作得很好的調試Nightmare.js

調試解決方案,如鐵節點電子基礎,他們不會與Nightmare.js代碼

工作

現在,你可以調用這樣的代碼:

test("https://yandex.com").then(console.log).catch(console.log) 
+0

我使用WebStorm但我只是從終端運行它。它掛起的原因是什麼(麻煩)?文檔沒有提到我看到的這個問題。 – xendi

+0

試試把這個添加爲env變量 - 「DEBUG = nightmare *」在Webstorm中,或者將它預先添加到你的終端命令 –

+0

我已經得到了惡夢API的工作,這要歸功於你的例子,但它仍然不會退出。我試着添加調試標誌。 – xendi