1. Web Page Scroll to a particular element:
Syntax: JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement ele = driver.findElement(By.id("123"));
js.executeScript("arguments[0].scrollIntoView(true);", ele);
Example:
class JavaScript
{
public static void main(String[] args)
{
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://testingcolleges.blogspot.com/p/testingform.html");
WebElement src = driver.findElement(By.linkText("Blogger"));
js.executeScript("arguments[0].scrollIntoView(true);", src);
}
}
2. Scroll to the Top:
Syntax: JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(document..body.scrollHeight , 0);");
Example:
class JavaScript
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver" , "path");
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://testingcolleges.blogspot.com/p/testingform.html");
js.executeScript("window.scrollTo(document.body.scrollHeight , 0);");
}
}
3. Scroll to the Down:
Syntax: JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0 , document..body.scrollHeight );");
Example:
class JavaScript
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver" , "path");
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://testingcolleges.blogspot.com/p/testingform.html");
js.executeScript("window.scrollTo(0 , document.body.scrollHeight);");
}
}
No comments:
Post a Comment