2016-10-25 38 views
2

這是我的代碼看起來像:CasperJS waitForSelector不添加「然後」步驟陣列

var firstFrameLoadingTime = 3000; 

firstFrameLoadingWaiter(function() { 
    casper.echo("callback"); 
}); 

function firstFrameLoadingWaiter(callback) { 
    casper.waitForSelector('div', 
     function suc() { 
      casper.echo('success!'); 
     }, 
     function timeout() {   
       casper.echo('failure!'); 
     }, 
     firstFrameLoadingTime); 
} 

的問題是,suc函數永遠不會被調用。我的意思是它沒有添加到CasperJS步驟的數組中。

這是日誌的一部分:

[18] => [info] [phantom] Step _step 5/5 https://... (HTTP 200) 
[19] => [info] [phantom] Step _step 5/5: done in 3392ms. 
[20] => [info] [phantom] waitFor() finished in 40ms. 
[21] => [info] [phantom] Done 5 steps in 3451ms 

如果頁面上找不到以前選擇自帶超時腳本就像一個魅力。

UPD。事實證明,問題出在do_whilewaitFor不兼容。

+0

請問您可以發佈您的所有代碼嗎? – Sayakiss

回答

0

經過一段時間的研究,我發現問題出在do_while修改step.then函數。 do_whilewaitFor函數不兼容。

解決方法(pull request)很簡單,但不是最清楚。 我剛剛命名的特殊方式的功能,並增加了一個小的檢查:

var isWaitSuccessFun = step.name.indexOf('successThen') != -1; 
if(isWaitSuccessFun || !this.steps[this.current].executed) { 

爲了讓您waitFor工作只是保證成功,函數名中包含successThen作爲例子:

casper.waitFor(
    function check(){ 
     // check if page is ready 
    }, 
    function successThen(){ 
     // execute after page is ready 
    }, 
    function(){ 
     // time out happened before page got ready 
    }, 
    timeOutTime 
); 

這也使得waitForSelector工作以及基於waitFor的其他類似功能。

完整的代碼在我的forked repository有效。

相關問題