Phantom JS加载更多元素

时间:2016-07-13 09:59:07

标签: javascript ajax web-scraping phantomjs

我需要废弃Facebook视频页面,但是需要加载更多按钮"在阿贾克斯。 所以我尝试使用PhantomJS点击按钮。

但是,我需要在按钮上多次单击。

所以这是我的代码:

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循环时代码不起作用,他的负载很好......

我希望有人可以帮助我!

1 个答案:

答案 0 :(得分:0)

问题解决了: 在循环时使用setInterval:

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);
相关问题