Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  • Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  • Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  • Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  • Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Images
  • Handling Mouse Events
  • Handling Keyboard Events
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
  • Types of wait statement in selenium
  • How to take screenshots?
  • How to upload File in Selenium?
  • How to handle scroll function using JavaScriptExecutor?
  • How to handle basic functionality using JavaScriptExecutor?
  • How to handle advanced functionality using JavaScriptExecutor?
  • How to automate CAPTCHA?
  • How to handle cookies in selenium
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • How to handle scroll function using JavaScriptExecutor?

How to handle scroll function using JavaScriptExecutor?

How to handle scroll function using

JavaScriptExecutor?

What is JavaScriptExecutor in Selenium?

JavaScriptExecutor is an interface in Selenium that lets you execute JavaScript code directly in the context of the browser.

It’s useful when:

  • Selenium’s regular API cannot interact with certain elements (hidden, complex, or dynamically loaded).
  • You need to perform actions like scrolling, clicking via JS, or retrieving JS-only data.

All WebDriver implementations (Chrome, Firefox, etc.) support it.

How to use:

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("alert('Hello from JS!');");

How to Handle Scroll Events in Selenium with JavaScriptExecutor?

1. Scroll to Bottom of the Page

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("window.scrollTo(0, document.body.scrollHeight);");

2. Scroll to a Specific WebElement

WebElement element = driver.findElement(By.id("targetElement"));

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("arguments[0].scrollIntoView(true);", element);

  • This scrolls the page until the specified element is visible in the viewport.

3. Scroll by Pixel Value (Vertical or Horizontal)

// Scroll down 500 pixels vertically

js.executeScript("window.scrollBy(0, 500);");

// Scroll right 300 pixels horizontally

js.executeScript("window.scrollBy(300, 0);");

Summary Table

Scroll ActionCode Example
Scroll to bottomjs.executeScript(“window.scrollTo(0, document.body.scrollHeight);”);
Scroll to elementjs.executeScript(“arguments[0].scrollIntoView(true);”, element);
Scroll by pixels (down/up)js.executeScript(“window.scrollBy(0, 500);”);
Scroll by pixels (right/left)js.executeScript(“window.scrollBy(300, 0);”);

Tip:


Always cast your driver to JavascriptExecutor as shown:

JavascriptExecutor js = (JavascriptExecutor) driver;

Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
How to upload File in Selenium?How to handle basic functionality using JavaScriptExecutor?
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105