Scroll By
Scroll in Selenium
To scroll using Selenium, you can use JavaScriptExecutor interface that helps to execute JavaScript methods through Selenium Webdriver
A Scrollbar is a lets you move around screen in horizontal or vertical direction
// This will scroll down the page by 1000 pixel vertical
js.executeScript("window.scrollBy(0,1000)");
------------------------------------------------------------
//Find element by link text and store in variable "Element"
WebElement Element = driver.findElement(By.linkText("Linux"));
//This will scroll the page till the element is found
js.executeScript("arguments[0].scrollIntoView();", Element);
-----------------------------------------------------------------------
//This will scroll the web page till end.
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
---------------------------------------------------------
WebElement Element = driver.findElement(By.linkText("VBScript"));
//This will scroll the page Horizontally till the element is found
js.executeScript("arguments[0].scrollIntoView();", Element);
-------------------------------------------------------
Scenario 3: To scroll down the web page at the bottom of the page.
Selenium Scriptimport org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class ScrollByPage { WebDriver driver; @Test public void ByPage() { System.setProperty("webdriver.chrome.driver", "E://Selenium//Selenium_Jars//chromedriver.exe"); driver = new ChromeDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; // Launch the application driver.get("http://demo.guru99.com/test/guru99home/"); //This will scroll the web page till end. js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); } }
js.executeScript("window.scrollBy(0,1000)");
------------------------------------------------------------
//Find element by link text and store in variable "Element"
WebElement Element = driver.findElement(By.linkText("Linux"));
//This will scroll the page till the element is found
js.executeScript("arguments[0].scrollIntoView();", Element);
-----------------------------------------------------------------------
//This will scroll the web page till end.
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
---------------------------------------------------------
WebElement Element = driver.findElement(By.linkText("VBScript"));
//This will scroll the page Horizontally till the element is found
js.executeScript("arguments[0].scrollIntoView();", Element);
-------------------------------------------------------
Scenario 3: To scroll down the web page at the bottom of the page.
Selenium Scriptimport org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class ScrollByPage { WebDriver driver; @Test public void ByPage() { System.setProperty("webdriver.chrome.driver", "E://Selenium//Selenium_Jars//chromedriver.exe"); driver = new ChromeDriver(); JavascriptExecutor js = (JavascriptExecutor) driver; // Launch the application driver.get("http://demo.guru99.com/test/guru99home/"); //This will scroll the web page till end. js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); } }
Comments
Post a Comment