Home > Enterprise >  Selenium Scroll Down Facebook Album Several Times
Selenium Scroll Down Facebook Album Several Times

Time:11-16

I try to scraping Facebook album but there is two scroll bar in the page and I want to know how to locating the inside scroll bar so it can automatically scroll down

I want to scroll the blue one

I try to use

driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")

but it doesn't work. I also tried

driver.find_element(By.TAG_NAME,"body").send_keys(Keys.PAGE_DOWN)

but I must click the scroll bar manually first to make it scroll.

CodePudding user response:

This command worked for me:

driver.execute_script("window.scrollBy(0, arguments[0]);", 600)

The entire code I used is:

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")
options.add_argument("--disable-infobars")
options.add_argument("start-maximized")
options.add_argument("--disable-extensions")

# Pass the argument 1 to allow and 2 to block
options.add_experimental_option(
    "prefs", {"profile.default_content_setting_values.notifications": 2}
)

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)

url = "https://www.facebook.com/"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='email']"))).send_keys(my_email)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='pass']"))).send_keys(my_password)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='login']"))).click()

time.sleep(1)
url = "https://www.facebook.com/media/set/?set=a.324091251402262&type=3"
driver.get(url)
for i in range(10):
    image = wait.until(EC.element_to_be_clickable((By.XPATH, "(//a[@aria-label='Photo album photo'])[last()]")))
    image.location_once_scrolled_into_view
    time.sleep(1)
  • Related