How to Scroll Page in Selenium?

FREE Online Courses: Knowledge Awaits – Click for Free Access!

Scrolling a web page is an essential feature when it comes to web automation testing. It is used to test the functionality of a website that requires scrolling, such as infinite scroll, lazy loading, and more. In this article, we will discuss how to scroll a web page using Selenium WebDriver.

What is Scrolling in Selenium?

Scrolling is the process of moving the visible area of a web page up or down. It is done to view the content that is not immediately visible on the screen. There are different ways to scroll a web page, including using the mouse wheel, keyboard, or touchpad. However, when automating tests with Selenium WebDriver, we use different techniques to simulate scrolling.

Scrolling a Web Page using Selenium WebDriver

Selenium WebDriver provides several methods to scroll a web page. These methods can be used to simulate different types of scrolling. Let’s take a closer look at each of these methods and how they can be used to scroll a web page.

1. Selenium scrollTo()

The scrollTo() method is used to scroll the web page to a specific location. This method takes two arguments: x and y coordinates of the location to which you want to scroll. To scroll to the bottom of the web page, you can set the y coordinate to a large value.

// Scroll to the bottom of the web page
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

2. Selenium scrollBy()

The scrollBy() method is used to scroll the web page by a specified number of pixels. This method takes two arguments: x and y coordinates of the number of pixels to scroll. To scroll down the web page by 100 pixels, you can set the y coordinate to 100.

// Scroll down the web page by 100 pixels
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,100)");

3. Selenium scrollIntoView()

The scrollIntoView() method is used to scroll the web page to an element that is not currently visible on the screen. This method takes one argument: the WebElement to which you want to scroll.

// Scroll to the element that is not currently visible on the screen
WebElement element = driver.findElement(By.id("element-id"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", element);

Selenium Scroll Example

Let’s look at an example of how to scroll a web page using Selenium WebDriver. Suppose we have a web application that uses infinite scroll to load more content when the user scrolls down the page. Here’s how we can simulate scrolling using Selenium WebDriver:

// Navigate to the web page
driver.get("https://www.example.com");

// Scroll down the web page multiple times
JavascriptExecutor js = (JavascriptExecutor) driver;
for (int i = 0; i < 5; i++) {
js.executeScript("window.scrollBy(0,1000)");
Thread.sleep(1000);
}

In this example, we first navigate to the web page using the driver.get() method. Then we simulate scrolling down the web page multiple times using the scrollBy() method provided by the JavaScriptExecutor interface. We use a loop to scroll down the web page five times, with each scroll movement moving the web page down by 1000 pixels. We also add a 1-second pause between each scroll movement using the Thread.sleep() method.

Conclusion

In conclusion, scrolling a web page is an essential feature when it comes to web automation testing. It is important to ensure that your tests can simulate scrolling accurately, especially when testing applications that require scrolling to view more content. In this article, we have discussed the different ways to scroll a web page using Selenium WebDriver. It includes using the scrollTo(), scrollBy(), and scrollIntoView() methods provided by the WebDriver API. We have also provided an example of how to simulate scrolling a web page using these methods.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *