Last modified: Nov 22, 2023 By Alexander Williams

Python Selenium: Scroll Down Slowly

Example 1: Scroll Down Using JavaScript with Smooth Behavior


from selenium import webdriver
import time

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Define the JavaScript code for scrolling
scroll_js = "window.scrollBy(0, 100);"

# Scroll down slowly in a loop
for _ in range(5):
    driver.execute_script(scroll_js)
    time.sleep(0.5)  # Adjust sleep duration as needed

# Close the browser
driver.quit()

Note: Adjust the `scrollBy(0, 100)` values and sleep duration based on your specific requirements.

Example 2: Scroll Down Using Actions Chains


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Scroll down slowly using Actions Chains
actions = ActionChains(driver)
for _ in range(5):
    actions.send_keys(Keys.ARROW_DOWN)
    actions.perform()
    time.sleep(0.5)  # Adjust sleep duration as needed

# Close the browser
driver.quit()

Output:


# No specific output. The script scrolls down slowly on the webpage.

Example 3: Scroll Down to Specific Element Using JavaScript


from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Find the target element
target_element = driver.find_element(By.XPATH, '//div[@id="target-element"]')

# Scroll down slowly to the target element
driver.execute_script("arguments[0].scrollIntoView({ behavior: 'smooth', block: 'center' });", target_element)
time.sleep(2)  # Adjust sleep duration as needed

# Close the browser
driver.quit()

Output:


# No specific output. The script scrolls down slowly to the target element on the webpage.

Example 4: Scroll Down Using Keys.ARROW_DOWN with Send_Keys


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Scroll down slowly using Keys.ARROW_DOWN with send_keys
for _ in range(5):
    driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.ARROW_DOWN)
    time.sleep(0.5)  # Adjust sleep duration as needed

# Close the browser
driver.quit()

Output:


# No specific output. The script scrolls down slowly on the webpage.

Example 5: Scroll Down to the Bottom of the Page


from selenium import webdriver
import time

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Scroll down to the bottom of the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)  # Adjust sleep duration as needed

# Close the browser
driver.quit()

Output:


# No specific output. The script scrolls down to the bottom of the webpage.

Example 6: Scroll Down Using Mouse Wheel


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

# Create a Chrome driver
driver = webdriver.Chrome()

# Navigate to a webpage
driver.get('https://example.com')

# Perform a slow scroll using the mouse wheel
actions = ActionChains(driver)
for _ in range(5):
    actions.move_by_offset(0, 100).perform()
    time.sleep(0.5)  # Adjust sleep duration as needed

# Close the browser
driver.quit()

Output:


# No specific output. The script performs a slow scroll using the mouse wheel on the webpage.