Home > Java > javaTutorial > How do you achieve page scrolling in Selenium WebDriver (Selenium 2)?

How do you achieve page scrolling in Selenium WebDriver (Selenium 2)?

Susan Sarandon
Release: 2024-11-23 07:44:10
Original
896 people have browsed it

How do you achieve page scrolling in Selenium WebDriver (Selenium 2)?

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

Scrolling Options

In addition to scrolling by a specific amount, you have several options for page scrolling:

  • Scroll Up:
jse.executeScript("window.scrollBy(0,-250)");
Copy after login
  • Scroll to Bottom:

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 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 do you achieve page scrolling in Selenium WebDriver (Selenium 2)?. 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