You are on page 1of 12

12/13/2018 Scroll UP or Down a page in Selenium Webdriver

(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

Scroll UP or Down a page in Selenium Webdriver


What is a Scrollbar?
A Scrollbar is a lets you move around screen in horizontal or vertical direction if the current
page scroll does not fit the visible area of the screen. It is used to move the window up and
down.

Selenium Webdriver does not require scroll to perform actions as it manipulates DOM. But in
certain web pages, elements only become visible once the user have scrolled to them. In
such cases scrolling may be necessary.

Scroll bar is of two type : Horizontal and vertical scroll bar as shown in below screenshot.

(/images/1/120817_0811_ScrollUPorD1.png)
https://www.guru99.com/scroll-up-down-selenium-webdriver.html 1/12
12/13/2018 Scroll UP or Down a page in Selenium Webdriver

(/images/1/120817_0811_ScrollUPorD2.png)

Scroll in Selenium
To scroll using Selenium, you can use JavaScriptExecutor interface that helps to execute
JavaScript methods through Selenium Webdriver

Learn more about JavaScriptExecutor (/execute-javascript-selenium-webdriver.html)

Syntax :

JavascriptExecutor js = (JavascriptExecutor) driver;


js.executeScript(Script,Arguments);

Script – This is the JavaScript that needs to execute.


Arguments – It is the arguments to the script. It's optional.

Selenium Script to scroll down the page

Let's, see the scroll down a web page using the selenium webdriver with following 3
scenarios :

Scenario 1: To scroll down the web page by pixel.


Scenario 2: To scroll down the web page by the visibility of the element.
Scenario 3: To scroll down the web page at the bottom of the page.
Scenario 4: Horizontal scroll on the web page.

Scenario 1: To scroll down the web page by pixel.

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 2/12
Selenium
12/13/2018 Script Scroll UP or Down a page in Selenium Webdriver

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class ScrollByPixel {

WebDriver driver;
@Test
public void ByPixel() {
System.setProperty("webdriver.chrome.driver", "E://Selenium//Selenium_Jars//chromed
river.exe");
driver = new ChromeDriver();

JavascriptExecutor js = (JavascriptExecutor) driver;

// Launch the application


driver.get("http://demo.guru99.com/test/guru99home/");

//To maximize the window. This code may not work with Selenium 3 jars. If script fa
ils you can remove the line below
driver.manage().window().maximize();

// This will scroll down the page by 1000 pixel vertical


js.executeScript("window.scrollBy(0,1000)");
}
}

Script Description: In the above code first we launch the given URL in Chrome browser.
Next, scroll the page by 1000 pixels through executeScript. Javascript method ScrollBy()
scrolls the web page to the specific number of pixels.

The syntax of ScrollBy() methods is :

executeScript("window.scrollBy(x-pixels,y-pixels)");

x-pixels is the number at x-axis, it moves to the left if number is positive and it move to the
right if number is negative .y-pixels is the number at y-axis, it moves to the down if number is
positive and it move to the up if number is in negative .

Example:

js.executeScript("window.scrollBy(0,1000)"); //Scroll vertically down by 1000 pixels

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 3/12
Output
12/13/2018 analysis : Here is the output Scroll
when you
UP or execute
Down the above
a page in Selenium script .
Webdriver

(/images/1/120817_0811_ScrollUPorD3.png)

Scenario 2: To scroll down the web page by the visibility of the element.

Selenium Script

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 4/12
12/13/2018 Scroll UP or Down a page in Selenium Webdriver
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.chrome.driver", "G://chromedriver.exe");
driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;

//Launch the application


driver.get("http://demo.guru99.com/test/guru99home/");

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


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

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


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

Script Description: In the above code, we first launch the given url in Chrome browser.
Next, scroll the page until the mentioned element is visible on the current page. Javascript
method scrollIntoView() scrolls the page until the mentioned element is in full view :

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

"arguments[0]" means first index of page starting at 0.

Where an " Element " is the locator on the web page.

Output analysis : Here is the output when you execute the above script .

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 5/12
12/13/2018 Scroll UP or Down a page in Selenium Webdriver

(/images/1/120817_0811_ScrollUPorD4.png)

Scenario 3: To scroll down the web page at the bottom of the page.

Selenium Script

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class ScrollByPage {

WebDriver driver;
@Test
public void ByPage() {
System.setProperty("webdriver.chrome.driver", "E://Selenium//Selenium_Jars//chromed
river.exe");
driver = new ChromeDriver();

JavascriptExecutor js = (JavascriptExecutor) driver;

// Launch the application


driver.get("http://demo.guru99.com/test/guru99home/");

//This will scroll the web page till end.


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

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 6/12
Script
12/13/2018Description : In the above code, Scroll we
UP orfirst
Downlaunch the given
a page in Selenium url in Chrome browser.
Webdriver

Next, scroll till the bottom of the page. Javascript method scrollTo() scroll the till the end of
the page .

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

"document.body.scrollHeight" returns the complete height of the body i.e web page.

Output analysis: Here is the output when you execute the above script.

(/images/1/120817_0811_ScrollUPorD5.png)

Scenario 4: Horizontal scroll on the web page.

Selenium Script

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 7/12
12/13/2018 Scroll UP or Down a page in Selenium Webdriver
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 HorizontalScroll {

WebDriver driver;
@Test
public void ScrollHorizontally() {
System.setProperty("webdriver.chrome.driver", "E://Selenium//Selenium_Jars//chromed
river.exe");
driver = new ChromeDriver();

JavascriptExecutor js = (JavascriptExecutor) driver;

// Launch the application


driver.get("http://demo.guru99.com/test/guru99home/scrolling.html");

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

//This will scroll the page Horizontally till the element is found
js.executeScript("arguments[0].scrollIntoView();", Element);
}
}

Script Description : In the above code, we first launch the given url in Chrome browser.
Next, scroll the page horizontally until the mentioned element is visible on the current page.
Javascript method scrollIntoView() scrolls the page until the mentioned element is in full view
:

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

Output analysis: Here is the output when you execute the above script.

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 8/12
12/13/2018 Scroll UP or Down a page in Selenium Webdriver

(/images/1/120817_0811_ScrollUPorD6.png)

Summary
In the above tutorial, we illustrate the scroll of the web page through different scenarios.
In the first scenario, we showed the scroll down on page by pixel.
In the second scenario, we showed the scroll down of page until the visible of the
element.
In the third scenario, we showed the scroll down of page at the bottom of the page.
In the fourth scenario, illustrated the horizontal scroll on the web page.

 Prev (/object-repository-selenium.html) Report a Bug


Next  (/sikuli-tutorial.html)

YOU MIGHT LIKE:

SELENIUM SELENIUM SELENIUM

(/store-variables-handling- (/take-screenshot- (/listeners-selenium-


selenium-ide.html) selenium-webdriver.html) webdriver.html)
(/store-variables- (/take-screenshot- (/listeners-

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 9/12
handling-selenium-
12/13/2018 selenium-webdriver.html) selenium-webdriver.html)
Scroll UP or Down a page in Selenium Webdriver

ide.html) How to Take Screenshot in TestNG Listeners in


Store Variables, Echo, Alert, Selenium WebDriver Selenium: ITestListener &
PopUp handling in Selenium (/take-screenshot-selenium- ITestResult Example
IDE webdriver.html) (/listeners-selenium-
(/store-variables-handling- webdriver.html)
selenium-ide.html)

SELENIUM SELENIUM SELENIUM

(/introduction-testng- (/alert-popup-handling- (/selenium-


groups.html) selenium.html) alternatives.html)
(/introduction- (/alert-popup- (/selenium-
testng-groups.html) handling- alternatives.html)
TestNG Groups: Include, selenium.html) Top 15 Selenium
Exclude with Example - Alert & Popup Window Alterna ves in 2018
Selenium Tutorial Handling in Selenium (/selenium-alternatives.html)
(/introduction-testng- WebDriver
groups.html) (/alert-popup-handling-
selenium.html)

Selenium Tutorials
42) SSL Certificate Error Handling (/ssl-certificate-error-handling-selenium.html)

43) Handling Ajax call (/handling-ajax-call-selenium-webdriver.html)

45) Execute JavaScript based code (/execute-javascript-selenium-webdriver.html)

46) Using Selenium with Python (/selenium-python.html)

47) Use intelliJ & Selenium (/intellij-selenium-webdriver.html)

52) Flash Testing with Selenium (/flash-testing-selenium.html)

54) Core Extensions (/selenium-core-extensions.html)

55) Using Apache Ant with Selenium (/using-apache-ant-with-selenium.html)

56) Using Selenium with Github (/selenium-github.html)

57) Handling Cookies (/handling-cookies-selenium-webdriver.html)

58) Using SoapUI with Selenium (/using-soapui-selenium.html)

59) XSLT Report in Selenium (/xslt-report-selenium.html)

60) Firefox Profile (/firefox-profile-selenium-webdriver.html)

61) Breakpoints and Startpoints (/breakpoints-startpoints-selenium.html)

62) Selenium Interview Questions (/top-100-selenium-interview-questions-answers.html)


https://www.guru99.com/scroll-up-down-selenium-webdriver.html 10/12
12/13/2018
63) Cucumber Scroll UP or Down a page in Selenium Webdriver
Selenium (/using-cucumber-selenium.html)

64) Drag & Drop Selenium (/drag-drop-selenium.html)

65) Selenium C# Webdriver (/selenium-csharp-tutorial.html)

66) Creating Object Repository (/object-repository-selenium.html)

67) Scroll UP or Down a page (/scroll-up-down-selenium-webdriver.html)

68) Sikuli Tutorial (/sikuli-tutorial.html)

71) Selenium vs HP UFT (QTP) (/alm-qtp-selenium-difference.html)

72) Selenium Alternatives (/selenium-alternatives.html)

 (https://www.facebook.com/guru99com/) 
(https://twitter.com/guru99com) 
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)

(https://forms.aweber.com/form/46/724807646.htm)

About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)

Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)

Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)

Execute online
https://www.guru99.com/scroll-up-down-selenium-webdriver.html 11/12
12/13/2018 Execute Java Online
Scroll(/try-java-editor.html)
UP or Down a page in Selenium Webdriver

Execute Javascript (/execute-javascript-online.html)


Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)

© Copyright - Guru99 2018


Privacy Policy (/privacy-policy.html)

https://www.guru99.com/scroll-up-down-selenium-webdriver.html 12/12