Page Scroll in Selenium WebDriver (Selenium 2)
In Selenium RC, page scrolling was performed using selenium.getEval("scrollBy(0, 250)"). To achieve the same functionality in Selenium WebDriver (Selenium 2), use the following equivalent code using JavaScriptExecutor:
WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.scrollBy(0,250)");
Scrolling Options
In addition to scrolling by a specific amount, you have several options for page scrolling:
jse.executeScript("window.scrollBy(0,-250)");
Using JavaScriptExecutor:
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Using Keys.CONTROL Keys.END:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);
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);
The above is the detailed content of How do you achieve page scrolling in Selenium WebDriver (Selenium 2)?. For more information, please follow other related articles on the PHP Chinese website!