How to perform automation to scroll up or down a page using selenium with c#. In this example, We will see how to automate the Scroll bar in selenium with c#. Check more Challenging Problems you may face during the automation.
When you visit a website or any application you can see a scroll bar it used 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#
Sometime 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 the javascript code “window.scrollTo(0, 0)”. To execute this line of code over the browser during the automation we need to use Javascript 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 a parameter. So we need the last value of the page as the y-axis. We can easily find that value using “document.body.scroll Height”.
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);