How to scroll up or down a page using selenium with c#

How to perform automation to scroll up or down a page using selenium with c#. Before start automation for this let’s see what is scroll bar? When you visit a website or any application you can see a scroll bar its using to scroll the contains top to bottom, Bottom to top, Left to right, Right to left.

scroll up or down a page using selenium with c#

Some time during the automation we need to scroll a page for a fix location, To or bottom Let’s see step by step how to scroll UP or down a page using selenium with c#

Scroll up a page using selenium with c#

To perform scroll operation we need to use java script code “window.scrollTo(0, 0)”. To execute this line of code over the browser during the automation we need to use Java script Executor below is the example to use it.

IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript("window.scrollTo(0, 0)");

Scroll down a page using selenium with c#

 Actually, We are passing axis value to scrollTo(x,y) as parameter. So we need last value of the page as y axis. We can easily find that value using “document.body.scrollHeight”.

IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight)");

Scroll down to visible an element using selenium with C#

 IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;

js.ExecuteScript("arguments[0].scrollIntoView();", Element");

Scroll a page vertically to visible and element using selenium with c#

var element = driver.FindElement(By.Id("identifier"));

var script = "arguments[0].scrollIntoView(true);";

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

js.ExecuteScript(script, element);

Add a Comment

Your email address will not be published. Required fields are marked *

twenty − sixteen =