Page scroll up or down in Selenium WebDriver (Selenium 2) using java
Scenario/Test steps:
1. Open a browser and navigate to TestURL
2. Scroll down some pixel and scroll up
For Scroll down:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)");
OR, you can do as follows:
jse.executeScript("scroll(0, 250);");
For Scroll up:
jse.executeScript("window.scrollBy(0,-250)");
OR,
jse.executeScript("scroll(0, -250);");
Scroll to the bottom of the page:
Scenario/Test steps:
1. Open a browser and navigate to TestURL
2. Scroll to the bottom of the page
Way 1: By using JavaScriptExecutor
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Way 2: By pressing ctrl+end
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);
Way 3: By using Java Robot class
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_END);
robot.keyRelease(KeyEvent.VK_END);
robot.keyRelease(KeyEvent.VK_CONTROL);
Scrolling to the bottom of a page:
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");