2016-07-13 24 views
0

我需要取消Facebook視頻頁面,但「加載更多按鈕」是在Ajax中。 所以我嘗試使用PhantomJS來點擊按鈕。Phantom JS加載更多元素

但是,我需要多次點擊按鈕。

所以這是我的代碼:

var page = require('webpage').create(); 

page.open("https://www.facebook.com/MisterVOnline/videos", function(status) { 
    if (status === "success") { 
     page.evaluate(function() { 
      while(document.querySelector(".uiMorePagerPrimary")){ 
       document.querySelector(".uiMorePagerPrimary").click(); 
       window.setTimeout(function() { 
       }, 5000); 
      } 
     }); 
     window.setTimeout(function() { 
      //console.log(page.content); 
      page.render('facebook.jpeg'); 
      phantom.exit(); 
     }, 5000); 
    } 
}); 

時沒有while循環,他做了一個很好的加載代碼不工作...

我希望有人能幫幫我!

回答

0

問題解決了: 使用的setInterval而不是while循環:

var myTime = setInterval(function(){ 
    if(document.querySelector(".uiMorePagerPrimary")!=null) 
    { 
     nbTry=0; 
     document.querySelector(".uiMorePagerPrimary").click(); 
    } 
    else 
    { 
     nbTry++; 
    } 
    if(nbTry>5) 
    { 
     clearInterval(myTime); 
     window.callPhantom({ exit: true }); 
    } 
}, 500);