簡體   English   中英

Selenium - 完成ajax加載自動滾動到頁面底部

[英]Selenium - complete ajax loading autoscroll to bottom of page

我有一個網頁,當你滾動到底部,然后通過ajax加載更多的結果。 在完成之前,您可以對此進行多次迭代。 有點像facebook那樣。

我正在嘗試編寫一個selenium腳本,以便一直到頁面的末尾,直到它完成。

像這樣的一半完成它..我只是不知道如何確定頁面是否在底部 - 所以我可以把它放在某種循環中?

我的嘗試

    By selBy = By.tagName("body");

    driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);
    System.out.println("Sleeping... wleepy");

    Thread.sleep(5000);
    driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);

    System.out.println("Sleeping... wleepy1");
    Thread.sleep(3000);

    //etc...

可能看起來像這樣?
hasScroll()不是一個真正的方法。 我把它放在那里展示我想要實現的目標

   while (driver.findElement(By.tagName("body")).hasScroll()) {
        driver.findElement(selBy).sendKeys(Keys.PAGE_DOWN);
        System.out.println("Sleeping... wleepy");
        Thread.sleep(2000);
   }

試試這種方法:

//Get total height
By selBy = By.tagName("body"); 
int initialHeight = driver.findElement(selBy).getSize().getHeight();
int currentHeight = 0;

while(initialHeight != currentHeight){
        initialHeight = driver.findElement(selBy).getSize().getHeight();

        //Scroll to bottom
        ((JavascriptExecutor) driver).executeScript("scroll(0," + initialHeight + ");");

        System.out.println("Sleeping... wleepy");
        Thread.sleep(2000);

        currentHeight = driver.findElement(selBy).getSize().getHeight();
}

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM