How to Webpage Scroll Down and Scroll Up using Selenium WebDriver ?

package SeleniumLearn.Com;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import com.gargoylesoftware.htmlunit.WebClient;
import org.testng.annotations.BeforeTest;

public class Scrollingonpages {
public WebDriver driver;
    
@Test (priority=1)// Scroll down
         public void ScrollDown() throws Exception {
         driver.get("http://www.seleniumlearn.com/selenium");
         driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
         JavascriptExecutor jse = (JavascriptExecutor)driver;
         jse.executeScript("scroll(0, 900)"); // Y value is scroll down
         Thread.sleep(5000);        
    }

@Test (priority=2)// Scroll up
       public void ScrollUP() throws Exception {
       driver.get("http://www.seleniumlearn.com/selenium");
       driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
       JavascriptExecutor jse = (JavascriptExecutor)driver;
       jse.executeScript("scroll(900, 0)"); // Y value is scroll down
  }

@Test (priority=3)//Infinite Scrill Down 
        public void InfiniteScrillDown() throws Exception {
        driver.get("http://www.seleniumlearn.com/selenium");
        driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
       {
        Thread.sleep(1000);
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
   } 
   }

@Test  (priority=4)// Infinite Scroll
        public void InfiniteScroll() throws Exception {
        driver.get("http://www.seleniumlearn.com/selenium");
        driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
        Actions act = new Actions(driver);
        for(int i=0;i<=25;i++)
       {
        Thread.sleep(1000);
        //driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
       act.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();   // Shourtcut key for Infinite scroll (Ctrl+End)
       }
}

@Test (priority=5)// Continious Scrolling
       public void ContiniousScrolling() throws Exception {
       driver.get("http://www.seleniumlearn.com/selenium");
       driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
      {
      for(int i=0;i<25;i++)
      {
      //driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); // It will scroll speed
      Thread.sleep(1000);
      driver.findElement(By.tagName("body")).sendKeys(Keys.DOWN); // wait for 1 second and Scrool
      }
      }  
      }    
@Test (priority=6)// //Indentify Loacator Element
        public void IndentifyLoacatorElement() throws Exception {
        driver.get("http://www.seleniumlearn.com/selenium");
        driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
        {
            WebClient element = (WebClient) driver.findElement(By.xpath("//*[@id='test']/p[10]/a"));
            Coordinates coordinate = ((Locatable)element).getCoordinates(); 
            coordinate.onPage(); 
            coordinate.inViewPort();
             }    
          }

@BeforeTest
         public void beforeTest() {
         driver = new FirefoxDriver();
         driver.manage().window().maximize();
       }

@AfterTest
       public void afterTest() {
  }
}

Tags: