10

I'm using Selenium with c#.

Selenium usually can automatically scroll down to the bottom of a web page to find elements but I having issues with a certain page which can increase in size.

Can anyone suggest code that will scroll down to the bottom of the page once it grows in size?

user2184530
  • 347
  • 2
  • 4
  • 7
  • Do all the elements load when the page loads, or does it load in a lazy manner just like facebook posts. If so then you can recognize the text that is displayed such 'Loading more posts' or something like that and then keep looking for the element. That is one way of doing it.What is your use case here?.Do you get a text that says something similar. – Madusudanan Sep 02 '13 at 12:44
  • There are a number of different headings on the page that display more details when clicked. When something within the heading is clicked (after the heading has been clicked) I get a timeout error (which I believe is caused by the browser not scrolling down to find the element). – user2184530 Sep 02 '13 at 12:57
  • Does it work manually.Can you post your HTML here,the one with the header.Things will get clearer. – Madusudanan Sep 02 '13 at 13:21
  • See if http://stackoverflow.com/questions/3401343/scroll-element-into-view-with-selenium helps – Alpha Sep 03 '13 at 06:33

4 Answers4

12

Try using javascript as described in this question

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight);");
Community
  • 1
  • 1
Brantley Blanchard
  • 1,208
  • 3
  • 14
  • 23
7

I know it's an old one, but it may be of someone's help. I came out with the following C# code:

    private void ScrollToBottom(IWebDriver driver)
    {
        long scrollHeight = 0;

        do
        {
            IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
            var newScrollHeight = (long) js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight); return document.body.scrollHeight;");

            if(newScrollHeight == scrollHeight)
            {
                break;
            }
            else
            {
                scrollHeight = newScrollHeight;
                Thread.Sleep(400);
            }
        } while (true);
    }
Georgi Vatsov
  • 428
  • 6
  • 10
  • This worked awesome for me. Thank you! I had something similar that wasn't working quite right... Thank you for sharing this. – Derek Foulk May 17 '22 at 21:21
1

An example in C# using .Net 4.5 and Selenium WebDriver 2.45

Just change the _url variable to point to your website and run.

I used the ChromeDriver but it should work with the other drivers as well.

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumScrollTest {

    internal static class Program {

        // Declare Selenium Web Driver
        private static IWebDriver _chromeDriver;

        private static String _url;

        private static void Main(string[] args) {
            // Instantiate URL
            _url = @"http://my.website.com/LazyLoadContent";

            // Instantiate Web Driver as ChromeDriver and set initial URL
            _chromeDriver = new ChromeDriver {Url = _url};

            // Instruct the WebDriver to wait X seconds for elements to load
            _chromeDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));

            // Instantiate JavaScript Executor using the web driver
            var jse = (IJavaScriptExecutor) _chromeDriver;

            // The minified JavaScript to execute
            const string script =
                "var timeId=setInterval(function(){window.scrollY<document.body.scrollHeight-window.screen.availHeight?window.scrollTo(0,document.body.scrollHeight):(clearInterval(timeId),window.scrollTo(0,0))},500);";

            // Start Scrolling
            jse.ExecuteScript(script);

            // Wait for user input
            Console.ReadKey();

            // Close the browser instance
            _chromeDriver.Close();

            // Close the ChromeDriver Server
            _chromeDriver.Quit();
        }
    }
}

If you've already a moderate understanding of Selenium and C#, the important bit is really the JavaScript. -Sourced from Cybermaxs, here

    var timeId = setInterval(function () {
        if (window.scrollY !== document.body.scrollHeight)
            window.scrollTo(0, document.body.scrollHeight);
        else
            clearInterval(timeId);
    }, 500);

The 500 above is the interval at which it will attempt scroll (in microseconds), adjust this as necessary. [1000 microseconds = 1 second]

Community
  • 1
  • 1
JerodG
  • 1,248
  • 1
  • 16
  • 34
0

Am sorry I don't work with c# but guess the logic would remain the same for any language. If it is a lazy load of the page then you can use Actions class to perform sending pagedown key option. If you get message like more items to load or no more items then you can identify this element. Put the page down option inside a while loop which performs page down until the condition is satisfied. This way you can completely load all the content of the page. Let me know if you need more help.

Vinay
  • 648
  • 4
  • 12