1

I'm having some trouble scrolling down an infinite scrolling page using Python Selenium. I'd like to scroll down this page so that I can click on to each film on the page. The code runs to the end and quits the driver but without scrolling down the page - what's wrong?

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")

driver.get("https://www.justwatch.com/uk/provider/bfi-player/movies")
scroll(driver)
driver.quit()
1
  • I'm trying to get the page to scroll down. I can lose this line of code if needed - my priority is just to get the page to scroll down. Commented Mar 16, 2021 at 21:54

2 Answers 2

1

Try to use this:

import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.justwatch.com/uk/provider/bfi-player/movies")
time.sleep(3)
driver.execute_script("document.body.getElementsByTagName('ion-content')[0].scrollToBottom();")
time.sleep(3)
driver.quit()
Sign up to request clarification or add additional context in comments.

2 Comments

this code raises this exception: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'scrollToBottom' of undefined
@Eddyhut weirdo, mine works good, try to open browser-> move to you url->def tools-> console and run that command: document.body.getElementsByTagName('ion-content')[0].scrollToBottom();
1
driver = webdriver.Chrome()


def scroll(driver):
    timeout = 5
    time.sleep(5)
    # Get scroll height
    scrollelement = driver.execute_script(
        "return document.querySelector('.tabs-inner [tab=\"popular\"] ion-content').shadowRoot.querySelector('[class=\"inner-scroll scroll-y\"]')")
    last_height = driver.execute_script(
        "return arguments[0].scrollHeight", scrollelement)
    new_height=0
    print(new_height, last_height)
    while True:
        # Scroll down to bottom
       
        driver.execute_script(
            "arguments[0].scrollTo(0, arguments[0].scrollHeight);", scrollelement)

        # load the website
        time.sleep(5)

        # Calculate new scroll height and compare with last scroll height
        new_height = driver.execute_script(
            "return arguments[0].scrollHeight", scrollelement)
        print(new_height,last_height)


driver.get("https://www.justwatch.com/uk/provider/bfi-player/movies")
scroll(driver)
driver.quit()

You should do the scroll on the scrollelement as the scrollbar is also an element in the page. But the scrollbar is in side shadowRoot so use execute_script

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.