Testing With Selenium WebDriver (82 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (72 Blogs)
SEE MORE

How to Scroll Down in Selenium: A Comprehensive Guide

Published on Oct 17,2024 8 Views

A passionate and knowledgeable tech enthusiast known for his expertise in the... A passionate and knowledgeable tech enthusiast known for his expertise in the world of technology and programming. With a deep-rooted passion for coding, Sarfaraz...

WebDriver is the foundational tool for building browser automation scripts. Along with WebDriver, a few other Selenium interfaces are SearchContext, Remote WebDriver, TakesScreenshot, and JavaScriptExecutor. This blog will discuss different scrolling methods in Selenium WebDriver using JavaScriptExecutor. We will understand how to scroll down in Selenium, scroll by some pixels, scroll until the element is in view, scroll to the bottom of the webpage, and scroll horizontally.

Introduction

Do you know how to scroll down in Selenium? Scrolling in Selenium is the movement of the cursor on a webpage. This is done to make the areas likely out of reach of the browser window to be seen. Because traditional mouse or keyboard motions are inapplicable in Selenium WebDriver automation scenarios, we employ JavaScriptExecutor to navigate up and down through the content or web pages.

Let us discuss a few scenarios to understand the importance of the scroll down on a webpage.

  • Finding and communicating with hidden elements: There are elements on websites that are initially hidden and only show up as you scroll down. To work with these components seamlessly and enter the viewport for efficient automation, the Selenium approach must incorporate the scroll down.
  • Unlimited scrolling: This feature on some websites where when a user scrolls down, new data loads dynamically. Selenium WebDriver requires scrolling down in automated testing scenarios to start loading more content.
  • Getting around drop-downs and menus: In Selenium, navigating around drop-down and menus, particularly those with many options, may require swiping down to quickly reach the desired item.
  • Page layout verification: To thoroughly inspect the look and arrangement of items across various page sections, it is necessary to scroll down in Selenium to verify the page layout.

Integrating the org.openqa.selenium.JavascriptExecutor package in your code is the first step towards implementing scroll down in Selenium, particularly to an element and other pertinent activities.

The JavaScriptExecutor for Selenium syntax, which is necessary to integrate scroll down in Selenium features and other related actions, is shown below:

  1. JavascriptExecutor js = (JavascriptExecutor) driver;
  2. js.executeScript(script,arguments);

The JavascriptExecutor interface is used by Selenium automation to run JavaScript code, which is a language that can communicate with HTML in a browser.

How to scroll down on a webpage in Selenium by defining the number of pixels?

How to scroll down in Selenium using actions class? To learn how to scroll to a specific place on the page by entering its exact location or coordinates, refer to the script below to perform the Selenium scroll-down action on the Firefox web browser.


import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

 

public class HandleScroll

{

 

@Test

public void scrollDown()

{

System.setProperty("webdriver.gecko.driver","D://Selenium    Environment//Drivers//geckodriver.exe");

WebDriver driver = new FirefoxDriver();

driver.navigate().to("Website URL");

 

//to perform Scroll on application using Selenium

JavascriptExecutor js = (JavascriptExecutor) driver;

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

}

}

Output: The Geckodriver of Firefox is initialized by the code. After that, the Firefox browser is opened and sent to the given website URL. The browser window scrolls down 350 pixels vertically  the website loads.

To scroll up, a user only needs to change the second parameter’s pixel value (350 in this case) to a negative value (-350).

Please refer to the script that follows:


import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

 

public class HandleScroll

{

@Test

public void scrollDown()

 

{

System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");

 

WebDriver driver = new FirefoxDriver();

driver.navigate().to("Website URL"); // Specify the website URL

 

//to perform Scroll on application using Selenium

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("window.scrollBy(0,-350)", "");

}

 

}

How to use Selenium to scroll down to an element and make it visible

 

See the Selenium script that follows.


import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

 

public class ScrollByVisibleElement {

 

WebDriver driver;

@Test

public void ByVisibleElement() {

System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");

WebDriver driver = new FirefoxDriver();

JavascriptExecutor js = (JavascriptExecutor) driver;

 

//Launch the application

driver.get("https://www.browserstack.com/guide/selenium-scroll-tutorial");

 

//Locating element by link text and store in variable "Element"

WebElement Element = driver.findElement(By.linkText("Try Selenium Testing For Free"));

 

// Scrolling down the page till the element is found

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

}

}

Output: When the above code runs, Firefox will open and the specified URL (the Selenium tutorial) will be navigated to.

Want to learn more about it? Check out the Selenium Course online today.

How to scroll down to an element in Selenium until it is visible?

To accomplish the Selenium scroll-down action on the Firefox browser, refer to the script below.


import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

 

public class HandleScroll

{

 

@Test

public void scrollDown()

{

System.setProperty("webdriver.gecko.driver","D://Selenium    Environment//Drivers//geckodriver.exe");

WebDriver driver = new FirefoxDriver();

driver.navigate().to("Website URL");

 

//to perform Scroll on application using Selenium

JavascriptExecutor js = (JavascriptExecutor) driver;

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

}

}

Output: The Geckodriver of Firefox is initialized by the code. After that, the Firefox browser is opened and sent to the given website URL. The browser window scrolls down 350 pixels vertically as the website loads.

To scroll up, a user only needs to change the second parameter’s pixel value (350 in this case) to a negative value (-350).

Please refer to the script that follows:


import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

 

public class HandleScroll

{

@Test

public void scrollDown()

 

{

System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");

 

WebDriver driver = new FirefoxDriver();

driver.navigate().to("Website URL"); // Specify the website URL

 

//to perform Scroll on application using Selenium

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("window.scrollBy(0,-350)", "");

}

 

}

How to scroll down in selenium to an element and make it visible has the script written below:


import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

 

public class ScrollByVisibleElement {

 

WebDriver driver;

@Test

public void ByVisibleElement() {

System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");

WebDriver driver = new FirefoxDriver();

JavascriptExecutor js = (JavascriptExecutor) driver;

 

//Launch the application

driver.get("https://www.browserstack.com/guide/selenium-scroll-tutorial");

 

//Locating element by link text and store in variable "Element"

WebElement Element = driver.findElement(By.linkText("Try Selenium Testing For Free"));

 

// Scrolling down the page till the element is found

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

}

}

Output: When the aforementioned code runs, Firefox will open and the specified URL (the Selenium tutorial) will be navigated to.

How to scroll down to the bottom of the webpage using Selenium?

How to scroll down in selenium is a commonly asked question while surfing a webpage. We shall ascertain the page’s height to accomplish a seamless scroll down in Selenium WebDriver. To get to the bottom of the page in Selenium, we shall scroll down after determining the page height. In Selenium automation testing, this technique is commonly employed because scrolling is necessary to perform pertinent actions on the WebElements within the Document Object Model (DOM).

Use the below Selenium script to understand how to scroll down to the bottom of the webpage.


import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

 

public class HandleScroll

 

{

@Test

public void scrollDown()

 

{

 

System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");

 

WebDriver driver = new FirefoxDriver();

driver.navigate().to("Website URL"); // Specify the Website URL

 

//to perform scroll on an application using Selenium

 

JavascriptExecutor js = (JavascriptExecutor) driver;

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

 

}

}

Output: The aforementioned code will retrieve the webpage’s maximum height from the Document Object Model, after which the scrollBy() function will scroll to the bottom.

How to scroll horizontally on a webpage to a specific web element using Selenium?

One of the Selenium interview questions commonly asked is how to scroll horizontally on a webpage to a specific web element using Selenium. There are situations in which horizontal page scrolling becomes essential while doing automated testing for websites or online applications. Especially when concentrating on horizontal scrolling, it could become necessary to scroll left or right on the page.

Use the below script to scroll horizontally on a webpage to a specific web element using Selenium:


import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

 

public class HandleScroll

{

 

@Test

public void ScrollHorizontally()

 

{

 

System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");

WebDriver driver = new FirefoxDriver();

 

JavascriptExecutor js = (JavascriptExecutor) driver;

 

// Launch the application

driver.get(" Website URL “); // Specify the website URL

 

WebElement Element = driver.findElement(By.linkText("Auto Testing"));

 

//This will scroll the page Horizontally till the element is found

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

 

}

 

}

Output: The aforementioned code launches Firefox and directs users to the given URL. After loading the webpage, Selenium automatically identifies the designated element and scrolls horizontally until it fills the browser window.

Conclusion

Scrollbar handling is critical when automating the test cases involving hidden elements: elements within a scrolling container, elements within an infinite scrolling region, or elements positioned off-screen. With the help of JavaScriptExecutor, the testers can work with scrolling both in the vertical and the horizontal plane. This will help in the practical testing of web and integrated applications without problems with the interaction of elements and dynamic content.

FAQs

How do I scroll down using sendKeys in Selenium?

In Selenium, scrolling down works by using sendKeys(Keys. PAGE_DOWN) on the body or any web element. This one outputs the “Page Down” key to drive the cursor down the selected webpage.

How to scroll down screen in Selenium?

To scroll down the screen, you can using JavaScriptExecutor and the script like window. or using ScrollDown to (0,pixels) or using SendKeys.SendWait(“{PgDn}”). These, scrolls down the viewport by a certain number of pixels or simply by the press of the “pagination down” key.

How do you scroll into an element in Selenium?

While scrolling to an element in Selenium, there is a concept of JavaScriptExecutor, where command arguments[0] are passed. To achieve the scroll into view, we have to use scrollIntoView(true) on the desired web element. This makes the element appear in the viewport, enabling users to interact with it at some point.

How to scroll to the bottom of a page with Selenium?

To scroll to the bottom of the page, we need to use the JavaScriptExecutor concept, where we need to write the script window. scrollTo (0, document. body. scrollHeight). This command takes the user to the bottom of the page when it is executed dynamically.

How to scroll down multiple times in Selenium?

This way, using JavaScriptExecutor in a loop with a window, you can scroll down multiple times. scrollBy (0, pixels) or by sending many sendKeys (Keys. PAGE_DOWN) to the body or an active element. This simulates continuous scrolling.

Upcoming Batches For Selenium Course
Course NameDateDetails
Selenium Course

Class Starts on 28th October,2024

28th October

MON-FRI (Weekday Batch)
View Details
Selenium Course

Class Starts on 23rd November,2024

23rd November

SAT&SUN (Weekend Batch)
View Details
Selenium Course

Class Starts on 25th November,2024

25th November

MON-FRI (Weekday Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to Scroll Down in Selenium: A Comprehensive Guide

edureka.co