This post was most recently updated on June 14th, 2019
Scroll can be done in two ways one is horizontal and another is vertical. In selenium scroll operation is done using JavaScriptExecutor.
Code to scroll vertically Down by 500 pixel:
1 2 3 |
WebDriver driver = new ChromeDriver(); JavascriptExecutor javaScriptExecutor= (JavascriptExecutor)driver; javaScriptExecutor.executeScript("window.scrollBy(0,500)"); |
Code to scroll vertically UP by 500 pixel:
1 2 3 |
WebDriver driver = new ChromeDriver(); JavascriptExecutor javaScriptExecutor= (JavascriptExecutor)driver; javaScriptExecutor.executeScript("window.scrollBy(0,-500)"); |
Code to scroll horizontally RIGHT by 500 pixel:
1 2 3 |
WebDriver driver = new ChromeDriver(); JavascriptExecutor javaScriptExecutor= (JavascriptExecutor)driver; javaScriptExecutor.executeScript("window.scrollBy(500,0)"); |
Code to scroll horizontally LEFT by 500 pixel:
1 2 3 |
WebDriver driver = new ChromeDriver(); JavascriptExecutor javaScriptExecutor= (JavascriptExecutor)driver; javaScriptExecutor.executeScript("window.scrollBy(-500,0)"); |
Note: We can provide any pixel based on our requirement.