0

I am trying to use selenium to scroll down infinitely this webpage https://gfycat.com/discover/trending-gifs

I try this code:

from selenium import webdriver
options = webdriver.ChromeOptions()

options.add_argument("start-maximized")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(options=options, executable_path=r"C:\chromedriver.exe")

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

driver.quit()

But no scroll down happened.

I also tried:

from selenium.webdriver.common.keys import Keys

for i in range(10):
     driver.find_element_by_css_selector('html').send_keys(Keys.END)

But no scroll down happened too.

khaled koubaa
  • 836
  • 3
  • 14
  • Since the page doesn't have a fixed scroll height, I would suggest to try something like - `for (int i = 0; i < 5; i++) { driver.executeScript('window.scrollBy(0,350)', ''); }` This shall at least scroll down 5 times, you can change the value in for loop and keep scrolling down. – Pritam Patil Sep 30 '20 at 15:57
  • Try this it should work `for i in range(10): driver.find_element_by_tag_name('body').send_keys(Keys.END) time.sleep(1)` – KunduK Sep 30 '20 at 16:43
  • https://stackoverflow.com/questions/20986631/how-can-i-scroll-a-web-page-using-selenium-webdriver-in-python for infinite scrolling. – Arundeep Chohan Sep 30 '20 at 19:26

1 Answers1

1

For infinite of Scrolling website you can using this methods of coding in Selenium as you can see I am using while for making infinite in addition you should be import time module for time out of loading website

def scroll(driver):
    timeout = 5

    # Get scroll height
    last_height = driver.execute_script("return document.body.scrollHeight")

    while True:
        # Scroll down to bottom
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # load the website
        time.sleep(5)

        # Calculate new scroll height and compare with last scroll height
        new_height = driver.execute_script("return document.body.scrollHeight")
onehamed
  • 127
  • 9