滚动查看div中所有动态添加的元素

时间:2016-01-26 00:04:16

标签: javascript scroll web-scraping infinite-scroll

我在搜索完成后试图抓取google play商店页面,即这一个: https://play.google.com/store/search?q=stackoverflow&c=apps

正如您所看到的,当您滚动到页面末尾时会加载新的应用,有时会出现“显示更多”的内容。按钮也。我正在制作一个需要完成所有这些滚动的书签,但我无法使其正常工作。这是我目前的代码:

var appContainerDiv = document.getElementsByClassName("card-list two-cards")[0];
var numberOfApps = appContainerDiv.childNodes.length;

var numberOfApps2 = numberOfApps;

do {
    numberOfApps = numberOfApps2;
    window.scrollTo(0, document.body.scrollHeight);

    if (document.getElementById('show-more-button') !== null) {
        document.getElementById('show-more-button').click();
    }

    numberOfApps2 = document.getElementsByClassName("card-list two-cards")[0].childNodes.length;
}
while (numberOfApps2 > numberOfApps);

appContainerDiv是div,其中所有应用都是嵌套的。

1 个答案:

答案 0 :(得分:0)

var delay = 2000;
var height = document.body.scrollHeight;
window.scrollTo(0, document.body.scrollHeight);
var interval = setInterval(function() {

    window.scrollTo(0, document.body.scrollHeight);

    if (document.getElementById('show-more-button') !== null) {
        document.getElementById('show-more-button').click();
    }
    if (height == document.body.scrollHeight) {
        clearInterval(interval);

        }

    } else {
        height = document.body.scrollHeight;
    }

}, delay);