Purpose:
(1) Slide the scroll bar of the page to the bottom of the page.
(2), slide the scroll bar of the page to an element of the page.
(3), the scroll bar of the sliding page moves down a certain number of pixels.
Tested webpage: http://v.sogou.com
Example code:
package cn.om.webdriverapi; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterMethod; public class TestScrolling { WebDriver driver; String url; // Slide the scroll bar of the page to the bottom of the page @Test(priority = 1) public void scrollingToBottomofAPage() { // Use the JavaScript scrollTo method and the document.body.scrollHeight parameter to move the scroll bar of the page to the bottom of the page ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)"); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace (); } } @Test(priority = 2) public void scrollingToElementofAPage() { // Find the location of the "TV" label WebElement element = driver.findElement(By.xpath(".//*[@id='container']/div[2]/div[2]/div[2]/div[1]/h3/a")); // Use JavaScript's scrollIntoView() method to scroll the scroll bar to the specified element position on the page ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace (); } } // The scroll bar of the sliding page moves down a certain amount of pixels @Test(priority = 3) public void scrollingByCoordinatesofAPage() { // Using the scrollBy method in JavaScript, use 0, 800 as the abscissa and ordinate parameters. // Slide the scroll bar of the page down 800 pixels vertically ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,800)"); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace (); } } @BeforeMethod public void beforeMethod() { url = "http://v.sogou.com"; System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); driver.get(url); } @AfterMethod public void afterMethod() { driver.quit(); } }