Scroll the Web Page to Bottom Using Python Selenium – Python Selenium Tutorial

By | December 1, 2020

We can use python selenium to scroll the web page. In this tutorial, we will show you how to use python selenium to scroll the web page to the end of it.

Preliminary

We should import selenium first.

from selenium import webdriver
import time

Then we can create a driver to scroll web page.

Create a browser driver

driver = None
driver = webdriver.Chrome(executable_path=r"E:\Program Files\chromedriver.exe")

url = 'https://www.tutorialexample.com/'

You should notice: E:\Program Files\chromedriver.exe is the path of chrome browser driver.

Use chrome driver to scroll the web page to the bottom

try:   
    driver.get(url)
    
    js = 'window.scrollTo(0, document.documentElement.scrollHeight)'
    driver.execute_script(js)
    time.sleep(3)
except Exception as e:
    print(e)
finally:
    if driver:
        driver.quit()

In this example, we will use driver.execute_script(js) to run a js script to control the chrome browser.

We can use js to scroll the web page to the end of it.

Run this code, you will find this code works.

Leave a Reply

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