【问题标题】:Scrolling an ajax page completely using selenium webdriver使用 selenium webdriver 完全滚动 ajax 页面
【发布时间】:2015-06-24 04:45:58
【问题描述】:

我正在尝试使用此代码完全滚动页面:

JavascriptExecutor js = (JavascriptExecutor) Browser;
js.executeScript("javascript:window.onload=toBottom();"+
                                           "function toBottom(){" +"window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +"document.body.scrollHeight,document.documentElement.clientHeight));" +"}");
js.executeScript("window.status = 'fail';");

//Attach the Ajax call back method
js.executeScript( "$(document).ajaxComplete(function() {" + "status = 'success';});");
js.executeScript("window.status = 'fail';");

//Attach the Ajax call back method
js.executeScript( "$(document).ajaxComplete(function() {" +"status = 'success';});");

此代码工作正常,第一次尝试滚动页面,但是当页面向下滚动时,页面上会出现新数据,并且此代码无法再次滚动它。 所以我需要的是有人会帮助我滚动页面直到结束,直到滚动完成。

我是否为此使用任何循环?

帮助/建议/响应将不胜感激!

【问题讨论】:

  • 页面示例?
  • 您可以考虑 twitter 或linkedin主页例如,当您向下滚动时出现更多推文。
  • 但该页面上没有“结束”
  • 在Linkedin主页上,更新完成后就结束了,不能再滚动了。但我只是引用了这些作为例子。
  • 请试一试

标签: java javascript windows selenium webdriver


【解决方案1】:

所以,我会这样做:

bool pageEnded = false;
while (!pageEnded) {
    JavascriptExecutor js = (JavascriptExecutor) Browser;
    js.executeScript("window.scrollTo(0, document.body.offsetHeight);");
    pageEnded = (String)js.executeScript("return document.readyState;") ? true;
}

【讨论】:

  • 谢谢。我会尝试并告诉你。
  • 它没有完全滚动还是什么?请告诉我你是怎么做的?也可能是你的页面正在使用一些 js 框架并且做了一些意想不到的事情。
  • 页面只向下滚动一次,然后再次停止。我只为此使用 JavascriptExecutor。它在这里显示错误: bool pageEnded = false; (针对 bool 它显示错误)此外,此行也有错误: pageEnded = (String)js.executeScript("return document.readyState;") ?真的; (对真错误)
【解决方案2】:

我有一个具有类似功能的页面和我之前回答的另一个问题。我不熟悉任何通用的方法来知道页面是否没有加载任何其他元素。在我的例子中,页面设计为在每个滚动中加载 40/80(忘记了确切的计数)元素。因为,在大多数情况下,我知道估计的滚动数量(因为我正在使用一家测试公司,并且我知道 db 中有多少元素存在)我可以估计滚动数量并执行以下操作来处理该页面。

public void ScrollPage(int counter)
{
    const string script =
        @"window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));";


    int count = 0;

    while (count != counter)
    {
       IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
        js.ExecuteScript(script);

        Thread.Sleep(500);

        count++;
    }
}

看我的另一个回答here

Java 等效代码

public void ScrollPage(int counter) throws InterruptedException {

        String script = "window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));";
        int count = 0;

        while (count != counter)
        {
            ((JavascriptExecutor)driver).executeScript(script);

            Thread.sleep(500);
            count++;
        }
    }

使用

ScrollPage(10);

在需要滚动的地方

【讨论】:

  • 嗨 Saifur,我尝试了你的 java 等效代码,但它不起作用。
  • 你能提供更多信息吗?还有什么例外?
  • 嗨 Saifur,它只是不会向下滚动页面。就例外而言,没有这样的例外。我已经尝试使用它的方法滚动页面。它只向下滚动了一次页面。
  • @FurqanUdDin 你能分享你正在使用的代码吗?可能还有页面的屏幕截图/截屏?
  • 嗨 Saifur,我可以手动向下滚动页面多次,比如 10 次以上
【解决方案3】:

执行此操作的最佳方法如下(在 Python 中实现):

import time

def loadFullPage(Timeout):
    reachedbottom = None
    while not reachedbottom:
        #scroll one pane down
        driver.execute_script("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
        time.sleep(Timeout)
        #check if the bottom is reached
        a = driver.execute_script("return document.documentElement.scrollTop;")
        b = driver.execute_script("return document.documentElement.scrollHeight - document.documentElement.clientHeight;")
        relativeHeight = a / b
        if(relativeHeight==1):
            reachedbottom = True

您必须为您的互联网连接找到一个有效的超时。 3 秒的超时对我来说效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-11-14
    • 2012-08-30
    相关资源
    最近更新 更多