Uncategorized

Code for Python Selenium – How to Scroll Down and Up on a Web Page

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
#REQUIRED FOR HEADLESS PART 1
from selenium.webdriver.chrome.options import Options
#REQUIRED FOR HEADLESS
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys

#Set location of Chrome Driver
s = Service('/usr/local/bin/chromedriver')

#Set some selenium chrome options
chromeOptions = Options()
chromeOptions.headless = False
driver = webdriver.Chrome(service=s, options=chromeOptions)

list_data = []

def initialize_browser():
    driver.get("InsertYOURURL")
    print("starting_Driver")
    time.sleep(1)
    driver.find_element(by=By.TAG_NAME, value="body").send_keys(Keys.PAGE_DOWN)
    time.sleep(1)
    driver.find_element(by=By.TAG_NAME, value="body").send_keys(Keys.PAGE_UP)
    time.sleep(1)
    driver.find_element(by=By.TAG_NAME, value="body").send_keys(Keys.PAGE_DOWN)
    time.sleep(1)
    driver.find_element(by=By.TAG_NAME, value="body").send_keys(Keys.PAGE_DOWN)
    time.sleep(1)
    driver.find_element(by=By.TAG_NAME, value="body").send_keys(Keys.PAGE_DOWN)
    time.sleep(1)
    driver.find_element(by=By.TAG_NAME, value="body").send_keys(Keys.PAGE_DOWN)
    time.sleep(1)

initialize_browser()

#driver.quit()