0

i'm using python selenium package and i'm trying to scroll a page to the bottom.. although, the site which i apply the selenium is dynamic (dynamic loading) which means it doesn't really scrolled to the bottom of the page.

after i read, and i find in the selenium documentation i was able to get that solution:

for i in range(25):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    time.sleep(0.5)

this is not good enough for me, because sometimes the page needs much more iterations, and sometimes much less for getting to the bottom of a page.

is there a get function so i can use if statement, something like:

for i in range(25):
    if driver.current_position == driver.document_height:
        break
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    time.sleep(0.5)

so, when i get to the end of the document i won't wait for nothing and break the loop.

or maybe if i can check how many pixels i jumped and if it's zero i will break the loop

omri_saadon
  • 10,193
  • 7
  • 33
  • 58

1 Answers1

0

It would be better to use a while loop, and break out of the loop once you hit the bottom of the page, rather than have a set number of iterations in a for There's a good discussion on one way to do this, here: Scrolling down a page with Selenium Webdriver

Community
  • 1
  • 1
Richard Horrocks
  • 419
  • 3
  • 19