How to scroll to element with Selenium WebDriver using C#?

We can scroll to an element with Selenium webdriver in C#. This is done with the help of JavaScript Executor. Selenium can run JavaScript commands with the help of ExecuteScript method.

The method scrollIntoView in JavaScript is used to perform the scrolling action and the value true is passed as a parameter to the method. This is then passed to the ExecuteScript method.


var e = driver.FindElement(By.XPath("//*[text()='Careers']"));
.ExecuteScript("arguments[0].scrollIntoView(true);", e);

For implementation we shall be using the NUnit framework.


using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
namespace NUnitTestProject1{
   public class Tests{
      String u = "";
      IWebDriver d;
      public void Setup(){
         //creating object of FirefoxDriver
         d = new FirefoxDriver();
      public void Test1(){
         //launching URL
         //identify element
         var e = d.FindElement(By.XPath("//*[text()='Careers']"));
         // JavaScript Executor to scroll to element
         .ExecuteScript("arguments[0].scrollIntoView(true);", e);
      public void close_Browser(){


Click on Run All Tests button −

Click on Open additional output for this result link −

We should get the Test Outcome and Standard Output.

Updated on: 30-Jan-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started