java - 使用 selenium webdriver 完全滚动 ajax 页面

标签 java javascript windows selenium webdriver

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

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';});");

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

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

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

最佳答案

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

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++;
        }
    }

Use

ScrollPage(10);

在需要滚动的地方

关于java - 使用 selenium webdriver 完全滚动 ajax 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29697402/

相关文章:

javascript - 任务完成后 Gulp 重新加载浏览器

javascript - BxSlider 上的链接在 Google Chrome 中不起作用,但在所有其他浏览器中都起作用

C、MS Windows系统创建临时文件

Windows 批处理文件包含所有错误级别

Java 十六进制到十进制转换 : Custom Logic

java - JdbcTemplate SELECT ... FOR UPDATE - 无锁

java - 为什么我在连接 java 和 ms Access 时出错

java - 如何实现多线程以调用多个数据的 REST URL

windows - NSIS 使用 GetParameters 的奇怪行为

javascript - 多个过滤器的集会查询不起作用