Selenium C# – JavaScript Scroll Up

Selenium C# – JavaScript Scroll Up
Introduction
In Selenium automation, sometimes you need to scroll back up to the top of the page after scrolling down to find an element. With JavaScriptExecutor in Selenium, you can easily scroll the window to any position, including the top. In this guide, we'll show you how to scroll up to the top using JavaScriptExecutor in Selenium with C#.
Code Implementation
Below is a C# Selenium script that opens a browser, navigates to a demo website, scrolls to a specific element using JavaScript, and then scrolls back to the top of the page.
Example: JavaScript Scroll Up with Selenium in C#.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using System.Globalization;
namespace SACSSAuto.QA.SeleniumPractice
{
[TestClass]
public class Selenium_77
{
/****
* Sample Code Definition :- Selenium - Scroll Up using JavaScript
* Developed By :- Pankaj Kumar
* Date of Creation :- 31-Jan-2025
* Project :- SACSSAUTO - C# [Web / Mobile / API] *
*/
[TestMethod]
public void Selenium_Test()
{
Console.WriteLine("Selenium - Scroll Up using JavaScript");
// Open Chrome Browser
IWebDriver driver = new ChromeDriver();
// Maximize the Browser Window
driver.Manage().Window.Maximize();
// Open the URL
driver.Navigate().GoToUrl("https://practice.solveautomation.in");
// Wait for 5 seconds
Thread.Sleep(5000);
//Click the Register Link
driver.FindElement(By.XPath("//a[normalize-space()='Register']")).Click();
// Wait for 5 Seconds
Thread.Sleep(5000);
// Click on Element By using JavaScript
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
IWebElement elem = driver.FindElement(By.XPath("//a[normalize-space()='SolveAutomation.in']"));
js.ExecuteScript("arguments[0].scrollIntoView(true);", elem);
Thread.Sleep(5000);
//Scroll to Top
js.ExecuteScript("window.scrollTo(0, 0);");
// Wait for 5 seconds
Thread.Sleep(5000);
// Quit the driver
driver.Quit();
}
}
}
Steps Explained
1. Navigating to the Target Page
The browser opens the practice site and clicks the "Register" link.
2. Scrolling Down and Up
JavaScriptExecutor scrolls to a specific link labeled "SolveAutomation.in" and then scrolls back to the top of the page.
3. Wait and Exit
After scrolling, the script waits briefly and closes the browser session.
Expected Outcome
Opens the practice website.
Navigates to the Register page.
Scrolls to the "SolveAutomation.in" link.
Scrolls back to the top of the page.
Closes the browser session.
Conclusion
Using JavaScriptExecutor's window.scrollTo(0, 0)
method lets you scroll to the top of the page automatically in Selenium automation. This handy trick is great for resetting the scroll position after you've interacted with elements lower down on the page, helping to keep the view consistent for the next steps.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
