简体   繁体   中英

Scrolling an ajax page completely using selenium webdriver

I am trying to scroll a page completely using this code:

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

This code works fine and scroll the page for the first attempt but when page is scrolled down, new data appears at the page and this code failed to scroll it again. So what I need is that someone will help me to scroll the page till end until scrolling is completed.

Do I use any loop for this?

Help/Suggestions/Response will be appreciated!

I had a page with similar functionality and another question I answered previously. I am not familiar with any generic way to know if page does not have any other elements on load. In my case the page is designed to load 40/80(forgot the exact count) element in each scroll. Since, most of the cases I know an estimated number of scroll(since I am using a test company and I know how many element present for that in db) I can estimate the number of scroll and did the following to handle that page.

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

See my other answer here

Java equivalency code

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);

in wherever the scroll is necessary

So, I would do something like that:

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

The best Way to do this is the following (implemented in 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

You have to find a efficient Timeout for your internet connection. A timeout of 3 seconds worked well for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM