How to Scroll web page in Selenium WebDriver – Complete Guide

rencontre badoo mayotte site de rencontre cuba gratuit How to Scroll web page in Selenium WebDriver

We can scroll the web page using JavaScript Executor in the java code. I have taken the below examples with Five different scenarios of scrolling a webpage.

1. Scroll Down:
2. Scroll up:
3. Scroll bottom of the Page:
4. Full scroll to bottom in slow motion:
5. Scroll automatically to your Web Element:

http://metodosalargarpene.es/ebioer/1721 1.Scroll Down:

package Sujoy;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class PageScrollDown {

 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  //Launching the browser application
  driver.get("http://www.testingbar.com/");
  //Adding wait
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  //Maximize window
  driver.manage().window().maximize();
  JavascriptExecutor jse = (JavascriptExecutor)driver;
//Scroll Down
  jse.executeScript("scroll(0, 250)"); //y value '250' can be altered
  }
}

http://aquanetta.pl/?kostromesp=opcje-binarne-5-minutowe&03a=30 Note: For Scrolling Horizontally You have to alter the value inside scroll() Method.

http://www.bicialpedrete.es/?okno=como-ligar-con-mujeres-de-40-a%D0%93%C2%B1os&52d=5e 2.Scroll up:

package Sujoy;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class PageScrollUp {

 public static void main(String[] args) throws InterruptedException {
  WebDriver driver = new FirefoxDriver();
  //Launching the browser application
  driver.get("http://www.testingbar.com/");
  //Adding wait
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  //Maximize window
  driver.manage().window().maximize();
  JavascriptExecutor jse = (JavascriptExecutor)driver;
//Scroll Down
  jse.executeScript("scroll(0, 650)"); //y value '650' can be altered
  Thread.sleep(5000);
//Scroll Up
  jse.executeScript("scroll(650, 0)"); //x value '650' can be altered


  }
}

http://www.gramus.si/ralf/5315 3.Scroll bottom of the Page:

package Sujoy;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class ScrollupToBottom {

 public static void main(String[] args) throws InterruptedException {
  WebDriver driver = new FirefoxDriver();
  //Launching the browser application
  driver.get("http://www.testingbar.com/");
  //Adding wait
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  //Maximize window
  driver.manage().window().maximize();
  JavascriptExecutor jse = (JavascriptExecutor)driver;
  jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");


  }
}

You can perform the same task using http://www.jcasoft.com/pidarmon/243 Actions Class Also:

Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();

rencontre en ligne et gratuit 4.Full scroll to bottom in slow motion:

package Sujoy;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class PageScrollSlowMotion {

 public static void main(String[] args) throws InterruptedException {
  WebDriver driver = new FirefoxDriver();
  //Launching the browser application
  driver.get("http://www.testingbar.com/");
  //Adding wait
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  //Maximize window
  driver.manage().window().maximize();
  for (int second = 0;; second++) {
      if(second >=20){ //You can alter the value '20' 
          break;
      }
          ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)", ""); //y value '800' can be altered
          Thread.sleep(3000);
}
  }
}

find out here 5.Scroll automatically to your WebElement:

package Sujoy;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class ScrollUpToElement {

 public static void main(String[] args) throws InterruptedException {
  WebDriver driver = new FirefoxDriver();
//Maximize window
  driver.manage().window().maximize();
  //Launching the browser application
  driver.get("http://www.testingbar.com/");
  //Adding wait
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  
  ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("//*[@id='wpcw_contact-4']/h4")));
//You have to provide the element’s Xpath 

  }
}