Home > Java > javaTutorial > body text

How to Scroll Pages Up and Down in Selenium WebDriver Using Java?

Patricia Arquette
Release: 2024-11-10 08:29:02
Original
832 people have browsed it

How to Scroll Pages Up and Down in Selenium WebDriver Using Java?

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

Scrolling Down

To scroll down a specific number of pixels, use:

jse.executeScript("window.scrollBy(0, 250)");
Copy after login

Alternatively, you can also use:

jse.executeScript("scroll(0, 250);");
Copy after login

Scrolling Up

To scroll up a specific number of pixels, use:

jse.executeScript("window.scrollBy(0, -250)");
Copy after login

Alternatively, you can also use:

jse.executeScript("scroll(0, -250);");
Copy after login

Scrolling to Bottom of Page

To scroll to the bottom of the page, you have three options:

  • Using JavaScriptExecutor (scrollHeight method):
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Copy after login
  • Pressing Ctrl End Keys:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.END);
Copy after login
  • 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);
Copy after login

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!

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