The Wayback Machine - https://web.archive.org/web/20240913191451/https://www.geeksforgeeks.org/how-to-scroll-down-to-bottom-of-page-in-selenium-using-javascriptexecutor/
Open In App

How to scroll down to bottom of page in selenium using JavaScriptExecutor

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS and its scripts are written in various languages i.e Java, Python, JavaScript, C#, etc. In this article, we will be working with Java and selenium to automate the scrolling of the webpage.

Scrolling is an important feature for any webpage, To scroll down the web pages we use the JavaScriptExecutor. The javascriptexecutor is an interface that enables to running of JavaScript methods from Selenium scripts.

Installation: To work with JavaScriptExecutor, we needed  Java Installed in our system and Selenium, a web driver for the browser, also an IDE for code editor.

  1. Eclipse IDE: Before downloading also make sure that your device has Java JDK. If you don’t have, install Java refers to this: How to Download and Install Java for 64-bit machine?. And install Eclipse IDE by referring to this article Eclipse IDE for Java Developers
  2. Selenium: Download the Selenium latest stable version here
  3. Web Driver: Download the Microsoft Edge webdriver according to your version here

Syntax:

  • JavaScriptExecutor 
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)", "");
  • Scroll Down to the bottom of the page:
window.scrollBy(0,document.body.scrollHeight)

Selenium runs the commands in Javascript with the execute_script() method. For scrolling down to the bottom of the page, we have to pass (0, document.body.scrollHeight) as parameters to the method scrollBy().

let us consider a test case in which we will try to automate the following scenarios in the Edge browser.

Step 1: Set a system property “webdriver.edge.driver” to the path of your EdgeDriver.exe file and instantiate an EdgeDriver class.

JavaScript
public class geeksforgeeks {
    public static void main(String args[]) {
        System.setProperty("webdriver.edge.driver", 
"C:\\Users\\ADMIN\\Documents\\Selenium\\msedgedriver.exe");
     // Instantiate a EdgeDriver class.     
     WebDriver driver = new EdgeDriver();
        //Maximize the browser  
    }
}

Step 2: Maximize the window: driver.manage().window().maximize()  and open the geeksforgeeks site.

JavaScript
driver.manage().window().maximize();
// Launch Website  
driver.get("https://www.geeksforgeeks.org/");  

Step 3: Initiate the javascriptexecutor and scroll method to scroll to the bottom of the page.

JavaScript
JavascriptExecutor js = (JavascriptExecutor) driver;
//Scroll down till the bottom of the page
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");

Example: Program to  scroll down to the bottom of the webpage using Selenium:

Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
public class geeksforgeeks {
    public static void main(String args[]) {
        System.setProperty("webdriver.edge.driver", 
"C:\\Users\\ADMIN\\Documents\\Selenium\\msedgedriver.exe");
     // Instantiate a EdgeDriver class.     
     WebDriver driver = new EdgeDriver();
        //Maximize the browser 
        driver.manage().window().maximize();
        // Launch Website  
        driver.get("https://www.geeksforgeeks.org/");  
    JavascriptExecutor js = (JavascriptExecutor) driver;
        //Scroll down till the bottom of the page
        js.executeScript("window.scrollBy(0,document.body.scrollHeight)");

    }
}

Output:

The above code starts the Edge browser and navigates to the geeksforgeeks page, once the webpage loads, Selenium automatically fetches the maximum height of the webpage from the Document Object Model, and then the scrollBy() method scrolls down to the bottom.



Similar Reads

How to scroll up and down in selenium by defining pixels using javascriptExecutor ?
In this article let's see how to scroll up and down using the javascriptexecutor. A scroll is a JavaScript method, the JavaScriptExecutor provides the interface that enables to use of the JavaScript methods from selenium scripts. Hence to scroll up or down in selenium JavaScriptExecutor is needed. Installation: To work with JavaScriptExecutor, we n
3 min read
JavaScriptExecutor in Selenium
Selenium is a popular open-source web-based automation tool. Sometimes, Selenium webdriver faces problems in interacting with a few web elements, For example, the user opens a website and there is an unexpected pop-up window that will prevent the webdriver from performing operations and produce inaccurate results. This is where JavascriptExecutor c
3 min read
How to show Toast at Bottom-left after Scroll Down in Bootstrap 5 ?
Toasts are quick, gentle pop-up messages that keep users informed without interrupting. To achieve this functionality bootstrap does not provide any built-in class. But we can achieve it by combining utility classes like position-fixed, bottom-0, and start-0, and you can position the toast at the bottom-left corner of the viewport. Syntaxif (scroll
3 min read
How to scroll the page up or down using anchor element in jQuery ?
The problem is to include a slide effect whenever we click a local anchor and we want to scroll up or down the page accordingly. Earlier we can do it natively by using CSS property. Syntax: a { scroll-behavior: smooth; } Now with the help of jQuery, we can do it by using the following two methods: jQuery.offset() method: This method allows us to re
2 min read
How to display scroll update when scrolling down the page using jQuery ?
In this article, we will learn to show scroll updates when scrolling down on a webpage. The scroll() event handler can be used to detect any scrolling in the page, but we only need to detect the scrolling down. The scroll down event can be handled using the scroll() and scrollTop() events. HTML DOM Window Scroll() Method: The scroll event is sent t
2 min read
How to scroll automatically to the Bottom of the Page using jQuery?
In this article, we will use the scrollTop() method of jQuery to scroll the page in different scenarios as discussed below: Table of Content Using the scrollTop with height() methodUsing scrollTop with animate() methodUsing the scrollTop with height() methodIn this approach, we will use the scrollTop() method with the height() method to scroll the
3 min read
Slide Down a Navigation Bar on Scroll using HTML, CSS and JavaScript
To create a slide down navigation bar you need to use HTML, CSS, and JavaScript. HTML will make the structure of the body, CSS will make it looks good. This kind of sliding navbar looks attractive on a site. By using JavaScript you can easily make the navigation bar slideable when the user scrolls down. Creating Structure: In this section, we will
4 min read
Hide scroll bar, but while still being able to scroll using CSS
Hiding the scrollbar with CSS enhances webpage aesthetics by removing visible scrollbars while maintaining scroll functionality. This can be achieved using browser-specific CSS properties for Chrome, Safari, Firefox, and Internet Explorer, ensuring a cleaner, more streamlined user interface without sacrificing usability. There are many other option
3 min read
How to set textarea scroll bar to bottom as a default using JavaScript/jQuery ?
Given an HTML document containing <textarea> element and the task is to set the position of scrollbar to bottom of <textarea> with the help of jQuery. Approach 1: Get the scrollHeight property of the textarea. Use scrollTop property to set the position of vertical scrollbar using jQuery. Example: This example implements the above approa
3 min read
How to set Bootstrap 4 scroll to the bottom of long sticky sidebar?
In Bootstrap 3, to set bootstrap scroll to the bottom of long sticky sidebar is possible using Affix which is handled by "Affix jQuery plugin". Unfortunately, in Bootstrap 4 migration "Affix jQuery plugin" was dropped. To achieve this affix property as like as Bootstrap 3, Bootstrap 4 recommends us CSS property i.e., position: sticky; directly in s
3 min read
CSS | Scroll Padding bottom
The Scroll Padding bottom is an inbuilt property in Scroll Snap module. This property set the scroll padding longhand. This property acts as a magnet on the top of the sliding element that stick to the top of the view-port and stop the scrolling(forcefully) in that place. The Scroll Padding property is optional, this property is used when the Scrol
2 min read
CSS scroll-margin-bottom Property
The scroll-margin-bottom property is used to set all the scroll margins to the bottom of an element at once. The value specified for the scroll-margin-bottom determines how much of the page that is primarily outside the support should remain visible. Hence, the scroll-margin-bottom values represent outsets that define the scroll snap area that is u
2 min read
How to create a Scroll To Bottom button in React JS ?
Learn to implement a "Scroll to Bottom" button in React using the useState() hook, a feature commonly seen in chat applications like WhatsApp and Telegram for effortless navigation to the end of a chat window. Prerequisites:NodeJS or NPMReact JSstyled-componentReact useState()Steps to Create the React Application And Installing Module:Step 1: Creat
2 min read
How to Scroll to Bottom of Div in JavaScript ?
Scrolling to the bottom of a div is a common task in web development, especially when dealing with chat applications, message boards, or any content where new information is frequently added at the bottom. There are several approaches to scrolling to the bottom of the div in JavaScript which are as follows: Table of Content Using scrollTop Property
4 min read
How to slide down the page after video ends using JavaScript ?
Given a web page, the task is to initiate the slider only after the video ends with JavaScript. Syntax In HTML: <video onended="myScript">In JavaScript: 1. Using custom function: video.onended=function(){myScript};2. Using the addEventListener() method: video.addEventListener("ended", myScript);Algorithm: Write the HTML part of the webpage an
4 min read
How to Scroll an Element into View in Selenium?
A high-level programming language that helps users in building web applications is called Java. It is not only used for creating web applications but it can also be used for automating web applications through various automation tools. Selenium is one such tool, which gives users the capability to automate from scratch to end, whether it is opening
4 min read
Create an Infinite Scroll Page using HTML CSS & JavaScript
In this article, we will create an infinite scroll page using HTML, CSS, and JavaScript. Infinite scrolling allows you to load and display content as the user scrolls down the page, providing a seamless browsing experience. We'll fetch and append new content dynamically as the user reaches the end of the current content. Preview Image: Approach:We'
2 min read
Scroll to the top of the page using JavaScript/jQuery
Have you ever find yourself wishing you could skip all that scrolling to get back to the top of a webpage? Well, good news! With JavaScript or jQuery, you can do just that. This article will walk you through the process. This trick will streamline your browsing experience. Say goodbye to endless scrolling and hello to instant page-top access! A scr
3 min read
How to make a Sidebar Sticky on Page Scroll using CSS ?
A Sticky Sidebar contains important links and navigation options fixed on the screen, making it easy for users to access them while scrolling down. This improves the user experience by saving them from scrolling back to the top of the page to find essential information. We have used two different ways to achieve this effect including, position: fix
4 min read
How to set the focus on drop-down list automatically when the page loads in HTML5?
The purpose of this article is to focus the drop-down list automatically when the page loads in HTML5. The <select> autofocus attribute is a boolean attribute that specifies that the drop-down list should automatically get focus when the page loads. Syntax: <select autofocus> Example: The following code demonstrates a drop-down list wit
1 min read
Angular PrimeNG Table Full Page Scroll
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Table Full Page Scroll in Angular PrimeNG. Angular PrimeNG Table Full Page Scroll enables Full Page Viewport scro
5 min read
Angular PrimeNG TreeTable Full Page Scroll
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use TreeTable Full Page Scroll in Angular PrimeNG. Angular PrimeNG TreeTable Full Page Scroll enables Full Page Viewp
7 min read
Angular PrimeNG Carousel Items Per Page and Scroll Items
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Carousel in Angular PrimeNG. Angular PrimeNG Basic Carousel is used to render the basic carousel. The carousel is
4 min read
How to make your page scroll to the top when route changes?
Clicking the "About Us" button in a React application doesn't automatically scroll to the top of the page load, as react-router-dom only changes the route without resetting the scroll position; a separate functional component is needed to ensure the new page loads from the top when the route changes. Prerequisite:ReactStyled-ComponentsuseEffect Rea
3 min read
How to Get All Available Links on the Page using Selenium in Java?
Selenium is an open-source Web-Automation tool that is used to automate web Browser Testing. The major advantage of using selenium is, that it supports all major web browsers and works on all major Operating Systems, and it supports writing scripts on various languages such as Java,  JavaScript, C# and Python, etc. While automating a webpage, we ar
2 min read
How to refresh a page using selenium JavaScript ?
Selenium is a tool with the help of it, we can perform automation testing in web browsers. According to the official documentation of Selenium - Selenium can automate anything present on the web. Automation scripts in Selenium can be written in multiple programming languages like C#, JavaScript, Java, Python, and a few others. In the process of web
2 min read
How to write a program that will return true if the bottom of the page is visible in JavaScript ?
The task is to return true if the bottom of the page is visible. We can achieve this by simply using the height of the window and the height of the scrolled window. Approach: Here we will first create a function that will be called whenever you use the scroll.Now we will check whether the bottom is visible each time when the function is called usin
1 min read
Selenium Scrolling a Web Page
Selenium is an open-source program that automates web browsers. It offers a single interface that enables us to create test scripts in several different programming languages, including Ruby, Java, NodeJS, PHP, Perl, Python, and C#. It is also compatible with different Operating Systems like Windows, Mac, Linux, etc. Selenium ecosystem consists of
6 min read
How to set page load timeout in Selenium?
Page load timeouts play a significant role in web development and user experience, acting as a safeguard against slow-loading pages and contributing to the overall success of a website or web application. Developers work diligently to manage and optimize load times to keep users engaged and satisfied. This article focuses on discussing how to set p
15+ min read
Selenium Program to Login to a Specific Web Page
Selenium RC (Selenium Remote Control) is one of the Selenium Components which was developed by Paul Hammant. It is an automation testing tool for web applications. To create test scripts, testers are allowed to use many programming languages such as Java, Python, Ruby, C#, JavaScript, Perl, and PHP. Here we are using Java for writing test scripts.
4 min read