Selenium - scroll up

I am using Selenium with C #.

Selenium can usually automatically scroll down to the bottom of a webpage to find items, but I have problems with a specific page, which can increase in size.

Can someone suggest a code that will scroll down to the bottom of the page when it grows in size?

+6
source share
4 answers

Try using javascript as described in this question

IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight);"); 
+8
source

I'm sorry that I do not work with C #, but I think that the logic will remain unchanged for any language. If this is lazy page loading, you can use the Actions class to send the pagedown key. If you receive a message that there are more items to load or more items, you can identify this item. Place the parameter down the page inside the while loop, which runs the page down until the condition is met. Thus, you can fully download the entire content of the page. Let me know if you need more help.

0
source

Example in C # using .Net 4.5 and Selenium WebDriver 2.45

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

I used ChromeDriver, but it should work with 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 already have a moderate understanding of Selenium and C #, the important bit is really JavaScript. -Used from Cybermaxs , here

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

500 above is the interval during which it will scroll (in microseconds), adjust it if necessary. [1000 microseconds = 1 second]

0
source

I know this is old, but it can help someone. 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); } 
0
source

Source: https://habr.com/ru/post/952973/


All Articles