Using JavaScript
Scroll Down:
import org.openqa.selenium.JavascriptExecutor;
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250)"); //y value '250' can be altered
Scroll up:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(250, 0)"); //x value '250' can be altered
Scroll bottom of the Page:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250)"); //y value '250' can be altered
Scroll up:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(250, 0)"); //x value '250' can be altered
Scroll bottom of the Page:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
(or)
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
Full scroll to bottom in slow motion:
for (int second = 0;; second++) {
if(second >=60){
break;
}
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)", ""); //y value '400' can be altered
Thread.sleep(3000);
}
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
Full scroll to bottom in slow motion:
for (int second = 0;; second++) {
if(second >=60){
break;
}
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)", ""); //y value '400' can be altered
Thread.sleep(3000);
}
(or)
JavascriptExecutor jse = (JavascriptExecutor)driver;
for (int second = 0;; second++) {
if(second >=60){
break;
}
jse.executeScript("window.scrollBy(0,800)", ""); //y value '800' can be altered
Thread.sleep(3000);
}
Scroll automatically to your WebElement:
Point hoverItem =driver.findElement(By.xpath("Value")).getLocation();
((JavascriptExecutor)driver).executeScript("return window.title;");
Thread.sleep(6000);
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+(hoverItem.getY())+");");
// Adjust your page view by making changes right over here (hoverItem.getY()-400)
for (int second = 0;; second++) {
if(second >=60){
break;
}
jse.executeScript("window.scrollBy(0,800)", ""); //y value '800' can be altered
Thread.sleep(3000);
}
Scroll automatically to your WebElement:
Point hoverItem =driver.findElement(By.xpath("Value")).getLocation();
((JavascriptExecutor)driver).executeScript("return window.title;");
Thread.sleep(6000);
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+(hoverItem.getY())+");");
// Adjust your page view by making changes right over here (hoverItem.getY()-400)
(or)
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",
driver.findElement(By.xpath("Value')]")));
Using KeyBoard
Using KeyBoard
We have two options
for scrolling in web page.
Using Actions Class
package name : org.openqa.selenium.interactions.Actions
java code :
Ctrl+End | Scroll to Bottom of the page
Actions actions = new
Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
Without Using Actions
Class
java code :
for(int i=0;i<10;i++)
{
driver.findElement(By.tagName("body")).sendKeys(Keys.DOWN);
}
Example On - Full scroll to bottom in slow motion
package
Others;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.JavascriptExecutor;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
import
org.testng.annotations.AfterTest;
import
org.testng.annotations.BeforeTest;
import
org.testng.annotations.Test;
public class
Dynamicalert
{
WebDriver driver;
@BeforeTest
public void
setup() throws Exception
{
System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");
driver = new
ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5,
TimeUnit.SECONDS);
driver.get("http://selenium-venkat.blogspot.com/p/index_4.html");
}
@AfterTest
public void
tearDown() throws Exception
{
driver.quit();
}
@Test
public void
Text() throws InterruptedException
{
for (int second =
0;; second++) {
if(second
>=60){
break;
}
((JavascriptExecutor)
driver).executeScript("window.scrollBy(0,400)", ""); //y
value '400' can be altered
Thread.sleep(3000);
}
}
}
awesome explanations
ReplyDelete