Scroll Page in Selenium:
What is a scrollbar?
- A scrollbar is one that lets you move around the screen in a horizontal or vertical direction if the current page is not fit in the viewing area of the scrolling screen.
- It is used to move the window up and down.
- Selenium Webdriver does not require scrolling to work because it combines DOM. But in some web pages, users only appear once, when users scroll them down scrolling in such cases may be necessary.
- The scroll bar has two types: horizontal and vertical scroll bars.
Multiple Ways to Scroll Down and Scroll Up page:
- Scroll Down and Scroll Up page using JavascriptExecutor.
- Scroll Down and Scroll Up page using Robot Class.
- Scroll Down and Scroll Up page Using Actions Class.
1. Scroll Down and Scroll Up page using JavascriptExecutor:
- Using JavascriptExecutor we can Scroll up and Scroll Down Web Page.
- Using window.scrollBy(0,250) OR scroll(0, 250) Methods we can Scroll Page.
Package:
1 | import org.openqa.selenium.JavascriptExecutor; |
Create JavascriptExecutor class object:
1 | JavascriptExecutor jse = (JavascriptExecutor)driver; |
Scroll Down:
1 2 3 4 5 | jse.executeScript("window.scrollBy(0,250)", ""); OR, you can do as follows: jse.executeScript("scroll(0, 250);"); |
Scroll Up:
1 2 3 4 5 | jse.executeScript("window.scrollBy(0,-250)", ""); OR, you can do as follows: jse.executeScript("scroll(0, -250);"); |
Example of Scroll Page in Selenium with JavaScriptExecutor class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class JavaScriptExecutor_Scroll { public static void main(String[] args) throws IOException { // Path of geckodriver System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Wait For Page To Load driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Go to URL driver.get("https://www.stqatools.com/"); // Maximize Window driver.manage().window().maximize(); // Create JavascriptExecutor object jes JavascriptExecutor jse = (JavascriptExecutor) driver; // Scroll Down page using First way jse.executeScript("window.scrollBy(0,250)", ""); // Scroll Up Page using First way jse.executeScript("window.scrollBy(0,-250)", ""); // OR // Scroll Down Page using Second way jse.executeScript("scroll(0, 250);"); // Scroll Up Page using Second way jse.executeScript("scroll(0, -250);"); } } |
2. Scroll Down and Scroll Up page using Robot Class:
- Using Robot Class we can Scroll up and Scroll Down Web Page.
- Using keyPress(KeyEvent.VK_PAGE_DOWN) Method we can Scroll Page.
Package:
1 2 3 | import java.awt.Robot; import java.awt.event.KeyEvent; |
Create Robot class object:
1 | Robot robot = new Robot(); |
Scroll Down:
1 2 3 | robot.keyPress(KeyEvent.VK_PAGE_DOWN); robot.keyRelease(KeyEvent.VK_PAGE_DOWN); |
Scroll Up:
1 2 3 | robot.keyPress(KeyEvent.VK_PAGE_UP); robot.keyRelease(KeyEvent.VK_PAGE_UP); |
Example of Scroll Page using Robot class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.awt.Robot; import java.awt.event.KeyEvent; import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Robot_Scroll { public static void main(String[] args) throws IOException { // Path of geckodriver System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Go to URL driver.get("https://www.stqatools.com/"); // Create a Robot class object Robot robot = new Robot(); // Scroll Down using Robot class robot.keyPress(KeyEvent.VK_PAGE_DOWN); robot.keyRelease(KeyEvent.VK_PAGE_DOWN); // Scroll Up using Robot class robot.keyPress(KeyEvent.VK_PAGE_UP); robot.keyRelease(KeyEvent.VK_PAGE_UP); } } |
3. Scroll Down and Scroll Up page Using Actions Class:
- Using Actions Class we can Scroll up and Scroll Down Web Page.
- Using keyDown(Keys.CONTROL).sendKeys(Keys.END) Method we can Scroll Page.
Package:
1 | import org.openqa.selenium.interactions.Actions; |
Create Action class object:
1 | Actions actions = new Actions(driver); |
Scroll Down:
1 | actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform(); |
Scroll Up:
1 | actions.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).perform(); |
Example to Scroll Using Actions class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.IOException; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Actions_Scroll { public static void main(String[] args) throws IOException { //Path of geckodriver System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); // Initialize WebDriver WebDriver driver = new FirefoxDriver(); // Go to URL driver.get("https://www.stqatools.com/"); // Create object of Actions class Actions actions = new Actions(driver); // Scroll Down using Actions class actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform(); // Scroll Up using Actions class actions.keyDown(Keys.CONTROL).sendKeys(Keys.HOME).perform(); } } |
How to Scroll Down or UP a Page in Selenium Webdriver and Vertical Scroll down and scroll up in Selenium WebDriver with java and How to scroll the Page up or down in Selenium WebDriver