Scrolling Down on a Selenium Web page
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class HandleScroll
{
@Test
public void scrollingDown()
{
System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("URL");
//to perform Scroll Down in our application
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)", "");
}
}
Output: A Firefox browser window is launched, accessing the specified website URL. Upon loading, the browser window is vertically scrolled down by 500 pixels.
Scrolling Up on a Selenium Web page
Similar to scrolling down we change the parameters of the scrollBy function to achieve an upward scroll
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class HandleScroll
{
@Test
public void scrollingUp()
{
System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("URL");
//to perform Scroll Up in our application
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,-500)", "");
}
}
Output: A Firefox browser window is launched, accessing the specified website URL. Upon loading, the browser window is vertically scrolled upwards by 500 pixels.
Scrolling down to an element until it is visible
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class ScrollToSelectedElement {
WebDriver driver;
@Test
public void ByVisibleElement() {
System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
//Launching the application
driver.get("A website’s URL");
//Storing the text element in a variable called "component"
WebElement component = driver.findElement(By.linkText("This is a sample text"));
// Scrolling down till the element is found
js.executeScript("arguments[0].scrollIntoView();", component);
}
}
Output: A Firefox browser window is launched, accessing the specified website URL. Upon loading, the browser window automatically navigates to where the component text is located.
Scrolling down to the bottom of the webpage
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class HandleScroll
{
@Test
public void scrollingToEnd()
{
System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("URL");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
}
}
Output: A Firefox browser window is launched, accessing the specified website URL. Upon loading, the browser window automatically navigates to the bottom of the webpage.
Frequently Asked Questions
-
How does Selenium handle scrolling?
The scrollBy() method involves two parameters, x, and y, representing the horizontal and vertical pixel values.
-
What is Selenium Grid?
Selenium Grid is an intelligent proxy server that makes it easy to run tests in parallel on multiple machines. This is accomplished by routing commands to remote web browser instances, where one server acts as the hub.
-
How do you scroll down a webpage in Selenium?
To scroll the web page, you can use JavascriptExecutor. Create an object for JavascriptExecutor and call the web driver. Then execute the script with a scrollTo function, and using that, you can either scroll to a particular section or the bottom of your page.
-
What is scroll into view?
The scrollIntoView() is a function used to scroll an element into the visible area of the browser window.
-
What does scroll down mean?
Scroll down is an action performed on a mobile or a desktop platform to move down to a particular section of a webpage.
Key Takeaways
In this article, we have extensively discussed Scrolling a webpage in Selenium.If you are Preparing for interview and don't know where to start, we have got you covered, check out our expert curated courses on our website, You can also check out Coding Ninjas Studio to practice frequently asked interview problems. We hope that this blog has helped you enhance your knowledge regarding Selenium and if you would like to learn more, check out our articles. Do upvote our blog to help other ninjas grow. Happy Coding!"