• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Scroll Down Using Selenium in Python

Scroll Down Using Selenium in Python

May 20, 2022 Leave a Comment

To scroll down using Selenium in Python, you can use the Selenium webdriver execute_script() function which executes JavaScript code in the browser.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

#Scroll to Bottom of Webpage
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")

The Selenium Python module gives you the tools you need to be able to automate many tasks when working with web browser.

When working with web browsers, it’s possible you may want to scroll down to the bottom of a webpage or scroll down to a particular pixel height.

To scroll down using Selenium in your Python code, you can use the Selenium webdriver execute_script() function. execute_script() is a versatile function and allows you to execute JavaScript code in the browser.

With JavaScript, we can easily scroll down in a browser.

How to Scroll Down to Bottom of Webpage Using Selenium

To scroll down to the bottom of a webpage using Selenium, you can use the execute_script() function to execute the JavaScript function window.scrollTo() and pass ‘document.body.scrollHeight’ for the second parameter.

Below shows you a simple example of how you can use Selenium to scroll down to the bottom of a webpage.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

#Scroll to Bottom of Webpage
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")

How to Scroll Down to Specific Height Using Selenium

If you want to scroll down to a specific pixel height, then you can change the second parameter of the window.scrollTo() function.

For example, if you want to scroll down to pixel 600, then pass ‘600’ to window.scrollTo().

Below shows you how to use Selenium to scroll down to a specific height in your Python code.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("http://theprogrammingexpert.com/")

#Scroll to Pixel 600 of Webpage
driver.execute_script("window.scrollTo(0,600))

Hopefully this article has been useful for you to learn how to scroll down using Selenium in Python.

Other Articles You'll Also Like:

  • 1.  How to Check if String Contains Uppercase Letters in Python
  • 2.  Format Numbers as Currency with Python
  • 3.  Multiple Condition While Loops in Python
  • 4.  Get Last Character in String in Python
  • 5.  pandas covariance – Calculate Covariance Matrix Using cov() Function
  • 6.  Using Selenium to Get Text from Element in Python
  • 7.  Python Get Yesterday’s Date
  • 8.  Euclidean Algorithm and Extended Euclidean Algorithm in Python
  • 9.  Python Check if Object Has Attribute
  • 10.  Reverse String with Recursion in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy