幻影JS加载更多元素

Phantom JS load more element

本文关键字:元素 加载 JS 幻影      更新时间:2023-09-26

我需要废弃Facebook视频页面,但"加载更多按钮"在Ajax中。所以我尝试使用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 循环时代码不起作用,他做了一个很好的加载......

我希望有人能帮助我!

问题已解决:使用 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);