Selenium

Selenium Scroll Tutorial – Horizontal & Vertical Scrolling using JavaScriptExecutor

Selenium Scroll Tutorial – Complete Guide 🚀

By Bhau Automation • Master Scrolling in Selenium WebDriver

🎯 What You Will Learn

  • How to scroll vertically (up & down)
  • How to scroll horizontally (left & right)
  • Using JavaScriptExecutor in Selenium
  • scrollIntoView() method usage
  • Real-world scrolling examples

📌 Why Scrolling is Important in Selenium?

Sometimes elements are not visible on the screen, so Selenium cannot interact with them. In such cases, scrolling is required to bring elements into view.

💡 Without scrolling, Selenium may throw ElementNotInteractableException.

⚙️ What is JavaScriptExecutor?

JavaScriptExecutor is an interface in Selenium that allows you to execute JavaScript code inside the browser.

  • Used for scrolling
  • Handling hidden elements
  • Performing advanced browser actions

⬇️ Vertical Scrolling (Down)

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)");

⬆️ Vertical Scrolling (Up)

js.executeScript("window.scrollBy(0,-500)");

➡️ Horizontal Scrolling (Right)

js.executeScript("window.scrollBy(500,0)");

⬅️ Horizontal Scrolling (Left)

js.executeScript("window.scrollBy(-500,0)");

🎯 Scroll Into View (Important)

WebElement element = driver.findElement(By.id("example"));
js.executeScript("arguments[0].scrollIntoView(true);", element);

This method scrolls the page until the element is visible.

🌍 Real-World Use Cases

  • Scrolling to load lazy content
  • Clicking hidden buttons
  • Testing long pages
  • Handling infinite scroll websites

🎥 Watch Full Video

👉 Watch Selenium Scroll Tutorial

🎓 Key Takeaways

  • Scrolling is essential for automation testing
  • JavaScriptExecutor helps control browser behavior
  • scrollIntoView is most useful method
  • Used in real-time automation frameworks

🚀 Created with ❤️ by Bhau Automation

Back to All Articles