Home > Java > javaTutorial > How to Scroll Pages in Selenium 2 with WebDriver (Java)?

How to Scroll Pages in Selenium 2 with WebDriver (Java)?

Susan Sarandon
Release: 2024-11-10 02:02:02
Original
1030 people have browsed it

How to Scroll Pages in Selenium 2 with WebDriver (Java)?

Page Scrolling in Selenium 2 with WebDriver (Java)

In Selenium 1, page scrolling was achieved using the selenium.getEval("scrollBy(0, 250)") method. In Selenium 2, the equivalent code is as follows:

Scrolling Down or Up

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;

// Scroll down
jse.executeScript("window.scrollBy(0,250)");
// OR
jse.executeScript("scroll(0, 250);");

// Scroll up
jse.executeScript("window.scrollBy(0,-250)");
// OR
jse.executeScript("scroll(0, -250);");
Copy after login

Scrolling to the Bottom of the Page

Using JavaScriptExecutor:

jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Copy after login

Using Keys.CONTROL Keys.END:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);
Copy after login

Using the 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);
Copy after login

The above is the detailed content of How to Scroll Pages in Selenium 2 with WebDriver (Java)?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template