Scroll by given pixel offset
//To scroll down web page by 900 pixels In x(vertical) direction.
//You can y parameter to scroll page In horizontal direction.
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("window.scrollBy(0,900)", "");
//To scroll up web page by 300 pixels In x(vertical) direction.
javascript.executeScript("window.scrollBy(0,-300)", "");
Scroll down to bottom of page
//Scroll down to bottom of the page.
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("window.scrollTo(0, document.body.scrollHeight)", "");
//Scroll till element.
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("arguments[0].scrollIntoView(true);",driver.findElement(By.partialLinkText("About Yahoo")));
Scroll to element using Keys
Actions action = new Actions(dr);
action.sendKeys(Keys.PAGE_DOWN);
Thread.sleep(5000);
action.click(driver.findElement(By.partialLinkText("About Yahoo"))).perform();
Example:
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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class ScrollDownUp {
public static void main(String[] args) throws InterruptedException {
// WebDriver reference but Firefox object
WebDriver dr = new FirefoxDriver();
//Maximize browser window
dr.manage().window().maximize();
//Go to URL
dr.get("https://www.yahoo.com/");
//Set timeout
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//To scroll down web page by 800 pixels In x(vertical) direction.
//You can y parameter to scroll page In horizontal direction.
JavascriptExecutor javascript = (JavascriptExecutor) dr;
javascript.executeScript("window.scrollBy(0,1200)", "");
Thread.sleep(3000);//
//To scroll up web page by 300 pixels In x(vertical) direction.
javascript.executeScript("window.scrollBy(0,-300)", "");
Thread.sleep(5000);
//Scroll down to bottom of the page.
JavascriptExecutor jasct = (JavascriptExecutor) dr;
jasct.executeScript("window.scrollTo(0, document.body.scrollHeight)", "");
Thread.sleep(5000);
//Scroll till element.
JavascriptExecutor je = (JavascriptExecutor) dr;
je.executeScript("arguments[0].scrollIntoView(true);", dr.findElement(By.partialLinkText("Mail")));
Thread.sleep(5000);
// Scroll till to element by Keys
dr.findElement(By.partialLinkText("About Yahoo")).sendKeys(Keys.PAGE_DOWN);
Thread.sleep(3000);
dr.findElement(By.partialLinkText("Mail")).sendKeys(Keys.PAGE_UP);
Thread.sleep(5000);
// Scroll till to element by action
Actions action = new Actions(dr);
action.sendKeys(Keys.PAGE_DOWN);
action.click(dr.findElement(By.partialLinkText("About Yahoo"))).perform();
//close firefox browser
dr.quit();
}
}
//To scroll down web page by 900 pixels In x(vertical) direction.
//You can y parameter to scroll page In horizontal direction.
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("window.scrollBy(0,900)", "");
//To scroll up web page by 300 pixels In x(vertical) direction.
javascript.executeScript("window.scrollBy(0,-300)", "");
Scroll down to bottom of page
//Scroll down to bottom of the page.
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("window.scrollTo(0, document.body.scrollHeight)", "");
//Scroll till element.
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("arguments[0].scrollIntoView(true);",driver.findElement(By.partialLinkText("About Yahoo")));
Scroll to element using Keys
Actions action = new Actions(dr);
action.sendKeys(Keys.PAGE_DOWN);
Thread.sleep(5000);
action.click(driver.findElement(By.partialLinkText("About Yahoo"))).perform();
Example:
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.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class ScrollDownUp {
public static void main(String[] args) throws InterruptedException {
// WebDriver reference but Firefox object
WebDriver dr = new FirefoxDriver();
//Maximize browser window
dr.manage().window().maximize();
//Go to URL
dr.get("https://www.yahoo.com/");
//Set timeout
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//To scroll down web page by 800 pixels In x(vertical) direction.
//You can y parameter to scroll page In horizontal direction.
JavascriptExecutor javascript = (JavascriptExecutor) dr;
javascript.executeScript("window.scrollBy(0,1200)", "");
Thread.sleep(3000);//
//To scroll up web page by 300 pixels In x(vertical) direction.
javascript.executeScript("window.scrollBy(0,-300)", "");
Thread.sleep(5000);
//Scroll down to bottom of the page.
JavascriptExecutor jasct = (JavascriptExecutor) dr;
jasct.executeScript("window.scrollTo(0, document.body.scrollHeight)", "");
Thread.sleep(5000);
//Scroll till element.
JavascriptExecutor je = (JavascriptExecutor) dr;
je.executeScript("arguments[0].scrollIntoView(true);", dr.findElement(By.partialLinkText("Mail")));
Thread.sleep(5000);
// Scroll till to element by Keys
dr.findElement(By.partialLinkText("About Yahoo")).sendKeys(Keys.PAGE_DOWN);
Thread.sleep(3000);
dr.findElement(By.partialLinkText("Mail")).sendKeys(Keys.PAGE_UP);
Thread.sleep(5000);
// Scroll till to element by action
Actions action = new Actions(dr);
action.sendKeys(Keys.PAGE_DOWN);
action.click(dr.findElement(By.partialLinkText("About Yahoo"))).perform();
//close firefox browser
dr.quit();
}
}
No comments:
Post a Comment