Scrolling Operations

In this article, we will learn how to do scrolling operation in Selenium.

To perform scrolling operation we need to use executeScript(); method of JavascriptExecutor interface.
Here we need to do casting of WebDriver interface to JavascriptExecutor interface.

JavascriptExecutor js = (JavascriptExecutor)driver; 

Type 1: To scroll down the web page by pixel:

This method is use to scroll the page by some specific number of pixels through executeScript. Javascript method ScrollBy() scrolls the web page to the specific number of pixels.

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

x-pixels is for horizontal scroll, it moves to the left if number is positive and it move to the right if number is negative .
y-pixels is for vertical scroll, it moves to the down if number is positive and it move to the up if number is in negative .

CODE
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScrollByPixel {
   public static void main(String[] args) {
	   
	   System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Files\\chromedriver.exe");
       WebDriver driver = new ChromeDriver();
       
       // Launch the application		
       driver.get("https://www.bookofnetwork.com/");

       //To maximize the window. 		
       driver.manage().window().maximize();

       JavascriptExecutor js = (JavascriptExecutor) driver;

       // This  will scroll down the page by  1200 pixel vertical		
       js.executeScript("window.scrollBy(0,1200)");

}
   
}

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

This method is use to 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.

Syntax
js.executeScript("arguments[0].scrollIntoView();",Element );
CODE
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;

public class ScrollIntoView {
   public static void main(String[] args) {
	   
	   System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Files\\chromedriver.exe");
       WebDriver driver = new ChromeDriver();
       
       // Launch the application		
       driver.get("https://www.bookofnetwork.com/");

       //To maximize the window.		
       driver.manage().window().maximize();

     //Find element by xpath and store in variable "Element"        		
       WebElement Element = driver.findElement(By.xpath("//div[@class=\"owl-item active\"]"));

       JavascriptExecutor js = (JavascriptExecutor) driver;

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

}
   
}

Type 3: To scroll down the web page at the bottom of the page:

This method is use to scroll till the bottom of the page. Javascript method scrollTo() scroll the till the end of the page .

Syntax
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
CODE
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ScrollToBottom {
   public static void main(String[] args) {
	   
	   System.setProperty("webdriver.chrome.driver", "C:\Users\Admin\Files\chromedriver.exe");
       WebDriver driver = new ChromeDriver();
       
       // Launch the application		
       driver.get("https://www.bookofnetwork.com/");

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

       JavascriptExecutor js = (JavascriptExecutor) driver;

     //This will scroll the web page till end.		
       js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

}
   
}
#How_to_do_Scrolling_operations_in_selenium_with_example #How_to_scroll_down_the_web_page_by_pixel_in_selenium #how_to_scroll_down_the_web_page_by_the_visibility_of_the_element_in_selenium #how_to_scroll_down_the_web_page_at_the_bottom_of_the_page_in_selenium

(New page will open, for Comment)

Not yet commented...