2

I performed code below on a floating page like https://www.youtube.com/user/JFlaMusic/videos with python selenium chromedriver. But code was performed incorrectly. I expected to go to the end of page, but last_height and new_height were same at first while condition loop. so break was performed. why does this result occur?

last_height = self.driver.execute_script("return document.documentElement.scrollHeight")
print(last_height)
while True:
    self.driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
    time.sleep(0.5)
    new_height = self.driver.execute_script("return document.documentElement.scrollHeight")
    print(new_height)
    if new_height == last_height:
        break
    last_height = new_height

1 Answer 1

1

Instead of using time.sleep(0.5) which looks quite unreliable, try to implement ExplicitWait as below to wait until page scrolled:

from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.common.exceptions import TimeoutException

last_height = self.driver.execute_script("return document.documentElement.scrollHeight")
print(last_height)
while True:
    self.driver.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
    try:
        wait(self.driver, 5).until(lambda driver: self.driver.execute_script("return document.documentElement.scrollHeight") > last_height)
    except TimeoutException:
        break
    last_height = self.driver.execute_script("return document.documentElement.scrollHeight")
Sign up to request clarification or add additional context in comments.

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.