【问题标题】:Selenium: Scroll to end of page in dynamically loading webpageSelenium:在动态加载网页中滚动到页面末尾
【发布时间】:2018-07-28 18:59:35
【问题描述】:

我的网页在向下滚动页面时会不断加载新项目,直到所有项目都加载完毕。

我正在使用 Java 中的 Selenium,需要向下滚动到页面底部才能加载所有内容。

我尝试了几种不同的选项,比如滚动到页面底部的某个元素:

WebElement copyrightAtEndOfPage = webDriver.findElement(By.xpath("//a[@href='/utils/copyright.html']"));
((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView();", copyrightAtEndOfPage);

这只是向下滚动一次,然后网页继续加载。

我也尝试了this 方法,它也只向下滚动一次,因为它只考虑浏览器高度。

非常感谢任何帮助。

【问题讨论】:

  • 我们也可以用JS做类似的事情吗?

标签: javascript java selenium


【解决方案1】:

我将为此提供 Python 代码。我认为翻译成 Java 很容易:

def scroll_down(self):
    """A method for scrolling the page."""

    # Get scroll height.
    last_height = self.driver.execute_script("return document.body.scrollHeight")

    while True:

        # Scroll down to the bottom.
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # Wait to load the page.
        time.sleep(2)

        # Calculate new scroll height and compare with last scroll height.
        new_height = self.driver.execute_script("return document.body.scrollHeight")

        if new_height == last_height:

            break

        last_height = new_height

希望对你有帮助!

【讨论】:

  • 效果很好,非常感谢!我还将用我翻译成 Java 的代码发布答案。
  • 做到了。再次感谢!
  • 嘿..感谢您的回答..这也应该帮助我!只是一个查询,当我使用这段代码时,我的浏览器会滚动到最后,但新元素仍然没有被捕获。我对此有点陌生。我正在使用 driver.get(url) 后跟上面的代码..你能帮忙吗?
  • @ShrutiJoshi,当您调用此函数时,此代码只会向下滚动到可见页面(屏幕上)的底部。
【解决方案2】:

感谢 Ratmir Asanov(请参阅上面批准的答案),我将 Python 代码翻译成 Java,以便其他人更容易实现。

try {
    long lastHeight = (long) ((JavascriptExecutor) webDriver).executeScript("return document.body.scrollHeight");

    while (true) {
        ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        Thread.sleep(2000);

        long newHeight = (long) ((JavascriptExecutor) webDriver).executeScript("return document.body.scrollHeight");
        if (newHeight == lastHeight) {
            break;
        }
        lastHeight = newHeight;
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}

【讨论】:

    【解决方案3】:

    稍微更新了 Johannes 代码以使其正常运行。

    JavascriptExecutor js = (JavascriptExecutor) driver;
    try {
        long lastHeight=((Number)js.executeScript("return document.body.scrollHeight")).longValue();
        while (true) {
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
            Thread.sleep(2000);
    
            long newHeight = ((Number)js.executeScript("return document.body.scrollHeight")).longValue();
            if (newHeight == lastHeight) {
                break;
            }
            lastHeight = newHeight;
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    

    【讨论】:

    • 类型转换是问题,它抛出编译错误。
    • 错误是什么?因为我从未遇到过该代码的任何问题,并且我在我的项目中以 1:1 的比例使用它。
    【解决方案4】:

    Prabhat 进一步更新上述解决方案,因为它仍然给我编译错误。

        try {
            Object lastHeight = ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
    
            while (true) {
                ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
                Thread.sleep(2000);
    
                Object newHeight = ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
                if (newHeight.equals(lastHeight)) {
                    break;
                }
                lastHeight = newHeight;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      【解决方案5】:

      我找到了另一个动态加载页面的解决方案。

      计算每次滚动前后显示的元素,并比较它们以确定您是否已滚动到底部。

      var reachedEnd = false;
      oldCount = driver.FindElements(By.CssSelector(".searchDataContainer.table-row.raw")).Count;
      
      while (!reachedEnd)
      {
          driver.FindElement(By.CssSelector("body")).SendKeys(Keys.End);
          Thread.Sleep(500);
          oldCount = driver.FindElements(By.CssSelector(".searchDataContainer.table-row.raw")).Count;
      
          if (newCount == oldCount)
          {
              reachedEnd = true;
          }
          else
          {
              newCount = oldCount;
          }
      }
      

      【讨论】:

        【解决方案6】:

        更新了对我有用的代码:

        try {
                            long lastHeight = (long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
                            int cont=1000;
                            while (true) {
                                ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, "+cont+");");
                                Thread.sleep(2000);
        
                                long newHeight = (long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
                                if (newHeight <= cont) {
                                    break;
                                }
        //                      lastHeight = newHeight;
                                cont+=500;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 2016-03-08
        • 1970-01-01
        • 2020-12-18
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多