How To Scroll Web page using Actions Class In Selenium
To Scroll Web page using Actions Class in Selenium WebDriver:
Let’s see how to Scroll Web Page using Actions Class in this post. There are few ways to scroll Web page UP or DOWN. We are going to see those in this post in detail.
To achieve this we use Actions class in Selenium WebDriver.
Must Read: Actions Class in Selenium WebDriver
Use sendKeys() method and pass parameters as PAGE_UP or PAGE_DOWN to achieve our required goal.
Scenario to be automated
- Launch the web browser and open the application – “https://www.softwaretestingmaterial.com”
- Do scroll down
- Do scroll up
- Close the browser
Given clear explanation in the comments section with in the program itself. Please go through it to understand the flow.
package softwareTestingMaterial; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.Test; public class ActionsClass { @Test public void actionsClass() throws InterruptedException{ System.setProperty("webdriver.chrome.driver", "D:\\Selenium Environment\\Drivers\\chromedriver.exe"); //creating an object 'driver' WebDriver driver = new ChromeDriver(); //Creating an object 'action' Actions action = new Actions(driver); //open SoftwareTestingMaterial.com driver.get("https://www.softwaretestingmaterial.com"); //sleep for 3secs to load the page Thread.sleep(3000); //SCROLL DOWN action.sendKeys(Keys.PAGE_DOWN).build().perform(); Thread.sleep(3000); //SCROLL UP action.sendKeys(Keys.PAGE_UP).build().perform(); //driver.close(); } }
This can be achieved by using JavascriptExecutor too.
See this: Page Up and Page Down Using JavascriptExecutor
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.