Scrolling Pages Up or Down in Selenium WebDriver Using Java
In Selenium 1 (a.k.a Selenium RC), page scrolling was performed using the selenium.getEval() method. To achieve the same functionality in Selenium 2 (WebDriver), the following equivalent code can be utilized:
WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver;
Scrolling Down
To scroll down a specific number of pixels, use:
jse.executeScript("window.scrollBy(0, 250)");
Alternatively, you can also use:
jse.executeScript("scroll(0, 250);");
Scrolling Up
To scroll up a specific number of pixels, use:
jse.executeScript("window.scrollBy(0, -250)");
Alternatively, you can also use:
jse.executeScript("scroll(0, -250);");
Scrolling to Bottom of Page
To scroll to the bottom of the page, you have three options:
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);
Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_END); robot.keyRelease(KeyEvent.VK_END); robot.keyRelease(KeyEvent.VK_CONTROL);
The above is the detailed content of How to Scroll Pages Up and Down in Selenium WebDriver Using Java?. For more information, please follow other related articles on the PHP Chinese website!