Selenium-完整的ajax加载自动滚动到页面底部
我有一个网页,当您滚动到底部时,它会通过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 heightBy 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();
}
希望对您有所帮助!
以上是 Selenium-完整的ajax加载自动滚动到页面底部 的全部内容, 来源链接: utcz.com/qa/429090.html

