0
votes

I have been using the below function to scroll down a page for over 2 years now and on the 31st December 2019 it just stopped working, no errors, just stopped scrolling down.

I'm using Chrome version Version 79.0.3945.88 and ChromeDriver 2.36.540470. Any ideas or help is greatly appreciated.

def scrollToEndOfPage(self, driver):
    try:
        time.sleep(1)

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

            # Wait to load page
            time.sleep(randint(2,4))

            # Calculate new scroll height and compare with last scroll height
            new_height = driver.execute_script("return document.body.scrollHeight;")
            if new_height == last_height:
                break
            last_height = new_height
    except Exception as e:
        print(str(e))

Update: 1

I've run document.body.scrollHeight; on the website in question (internal site) and it displays the page height but when I try and execute driver.execute_script("return document.body.scrollHeight;") via a script, it hangs on this request and doesn't return anything and there are no errors.

1
Can you share the URL? Is it possible site changed? - Sers
@Sers , the site hasn't changed, it's an internal system not accessible publicly, I've confirmed the site is still there and that nothing has changed, I've commented out the line to make it run chrome headless so I can see everything it's doing and when it gets to this function it just stays on the page, no errors, the script keeps running as if on hold until I kill the script. I wasn't able to find any known bugs in webkit that might cause it. - llanato
@Sers, very odd. - llanato
I tested your scroll and it's working on https://www.morhipo.com/kampanya/alisveris. Chrome 79.0.3945.88. ChromeDriver 79.0.3945.36. MacOS - Sers
@sers I've done some further testing, I've updated the original question about with Update: 1. - llanato

1 Answers

0
votes

You can try to wait for the page to be fully loaded before scrolling. For that you can use code below to wait for JavaScript to finish:

from selenium.webdriver.support.ui import WebDriverWait

# ...

WebDriverWait(browser, 30).until(lambda d: d.execute_script(
         'return (document.readyState == "complete" || document.readyState == "interactive")'))

Or use WebDriverWait and wait visibility/clickable of specific element/elements like below:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)

wait.until(EC.visibility_of_all_elements_located((By.XPATH, "some elements on locator")))
# or
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "some clickable element locator")))