Friday, August 30, 2013

Scroll Actions on the page


JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("window.scrollBy(0,500)", "");
 
  1. //Vertical scroll - down by 100 pixels                      jse.executeScript("window.scrollBy(0,100)", "");
  2. //Vertical scroll - up by 55 pixels (note the number is minus 55)                            jse.executeScript("window.scrollBy(0,-55)", ""); 
  3. //Horizontal scroll - right by 20 pixels                                                              jse.executeScript("window.scrollBy(20,0)", ""); 
  4. //Horizontal scroll - left by 95 pixels (note the number is minus 95)                     jse.executeScript("window.scrollBy(-95,0)", ""); 
  5. //Vertical scroll - Down End                                                                                jse.executeScript("window.scrollTo(0, document.body.clientHeight)");
  6. //Horizontal scroll - Top End                                                                        jse.executeScript("window.scrollTo (0, 0);");                                            (OR) jse.executeScript("window.scrollTo(document.body.scrollHeight, 0)");
  7. //Horizontal scroll - Down End                                                                                jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
  8. //Horizontal scroll Ajax Page                                                                    jse.executeScript("window.scrollTo(0, Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight))");

Scroll until the element is in view

public static void scrollToElement(WebElement element, WebDriver driver) {     JavascriptExecutor jse = (JavascriptExecutor) driver;     jse.executeScript("arguments[0].scrollIntoView(true);", element); }
Scroll top of the element to the top of the screen
WebElement elem = driver.findElement(By.id("some-id"));
JavascriptExecutor js = (JavascriptExecutor)driver;
//Scroll to top
js.executeScript("arguments[0].scrollIntoView()", elem);

Scroll to bottom of the screen
WebElement elem = driver.findElement(By.id("some-id"));
JavascriptExecutor js = (JavascriptExecutor)driver;
//Scroll to bottom
js.executeScript("arguments[0].scrollIntoView(false)", elem);

Scroll to mid/center of the screen
WebElement elem = driver.findElement(By.id("some-id"));
JavascriptExecutor js = (JavascriptExecutor)driver;
//Scroll to bottom
js.executeScript("arguments[0].scrollIntoView({behavior: "smooth", block: "center", inline: "nearest"}))", elem); 

No comments:

Post a Comment